久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python素數(shù)之和

python sum of primes(python素數(shù)之和)
本文介紹了python素數(shù)之和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想做一個python程序來生成一個數(shù)的質(zhì)數(shù)之和,但是程序沒有給出正確的結(jié)果,請告訴我為什么.

I am tying to make a python program that will generate the sum of primes for a number, but the program is not giving the correct result,please tell me why.

b=1
#generates a list of numbers.
while b<100:
    b=b+1
    x = 0.0
    a = 0
    d = 0
    #generates a list of numbers less than b. 
    while x<b:
        x=x+1
        #this will check for divisors. 
        if (b/x)-int(b/x) == 0.0:
            a=a+1
        if a==2:
            #if it finds a prime it will add it.
            d=d+b
print d 

我讓它成功生成了一個素數(shù)列表,但我無法添加素數(shù).

I made it generate a list of primes successfully, but i could not get the primes to add.

這是我用來生成素數(shù)列表的代碼.

This is the code that i used to generate a list of primes.

b=1
while b<1000:
    b=b+1
    n = b
    x = 0.0
    a = 0
    while x<n:
        x=x+1
        if (n/x)-int(n/x) == 0.0:
            a=a+1
    if a==2:
        print b

推薦答案

您的 d 變量在您的外部循環(huán)的每次迭代中被重置.將初始化移出該循環(huán).

Your d variable is being reset during each iteration of your outer loop. Move the initialization out of that loop.

此外,a == 2 檢查應(yīng)該只在外循環(huán)的每次迭代中發(fā)生一次.將其移出內(nèi)循環(huán).

Additionally, the a == 2 check should only occur once per iteration of the outer loop. Move it out of the inner loop.

b=1
d = 0
#generates a list of numbers.
while b<100:
    b=b+1
    x = 0.0
    a = 0
    #generates a list of numbers less than b. 
    while x<b:
        x=x+1
        #this will check for divisors. 
        if (b/x)-int(b/x) == 0.0:
            a=a+1
    if a==2:
        #if it finds a prime it will add it.
        d=d+b
print d 

結(jié)果:

1060

在我們處理它的同時,讓我們嘗試清理代碼以使其更易于理解.您可以將內(nèi)部循環(huán)移動到自己的函數(shù)中,以便讀者更清楚地了解其用途:

While we're at it, let's try cleaning up the code so it's more comprehensible. You can move the inner loop into its own function, so readers can more clearly understand its purpose:

def is_prime(b):
    x = 0.0
    a = 0
    while x<b:
        x=x+1
        #this will check for divisors. 
        if (b/x)-int(b/x) == 0.0:
            a=a+1
    if a==2:
        return True
    else:
        return False

b=1
d=0
#generates a list of numbers.
while b<100:
    b=b+1
    if is_prime(b):
        d=d+b
print d

使用描述它們所代表的變量名稱也很有用:

It's also useful to use variable names that describe what they represent:

def is_prime(number):
    candidate_factor = 0
    amount_of_factors = 0
    while candidate_factor<number:
        #A += B is equivalent to A = A + B
        candidate_factor += 1
        #A little easier way of testing whether one number divides another evenly
        if number % candidate_factor == 0:
            amount_of_factors += 1
    if amount_of_factors == 2:
        return True
    else:
        return False

number=1
prime_total=0
#generates a list of numbers.
while number<100:
    number += 1
    if is_prime(number):
        prime_total += number
print prime_total

for 循環(huán)比增加計數(shù)器的 while 循環(huán)更符合規(guī)范:

for loops are more idomatic than while loops that increment a counter:

def is_prime(number):
    amount_of_factors = 0
    for candidate_factor in range(1, number+1):
        if number % candidate_factor == 0:
            amount_of_factors += 1
    if amount_of_factors == 2:
        return True
    else:
        return False

prime_total=0
#generates a list of numbers.
for number in range(2, 101):
    if is_prime(number):
        prime_total += number
print prime_total

如果您覺得大膽,可以使用列表推導(dǎo)來減少您使用的循環(huán)數(shù)量:

If you're feeling bold, you can use list comprehensions to cut down on the number of loops you use:

def is_prime(number):
    factors = [candidate_factor for candidate_factor in range(1, number+1) if number % candidate_factor == 0]
    return len(factors) == 2

#generates a list of numbers.
primes = [number for number in range(2, 101) if is_prime(number)]
prime_total = sum(primes)
print prime_total

這篇關(guān)于python素數(shù)之和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 成人欧美一区二区三区黑人孕妇 | 欧美日韩在线一区二区 | 国产成人免费在线视频 | 性色av一区二区三区 | 国产一级二级三级 | 国产又粗又长又爽 | 日韩1级片 | 日韩免费视频 | 91亚洲精品乱码久久久久久蜜桃 | 麻豆成人91精品二区三区 | 成人小视频在线 | 能看的av网站 | 天天操天天拍 | 98国产精品| av中文在线| 国产精品黄 | 国产尤物视频 | 午夜你懂的 | 亚洲一区二区免费 | 欧美成人精品欧美一级乱黄 | 日韩欧美国产综合 | 亚洲精品国产精品国自产观看浪潮 | 国产精品伦子伦免费视频 | 精品在线免费视频 | av高清在线观看 | 思思在线视频 | 中文字幕永久 | 免费网站www| 亚洲精品少妇 | 成人久久av | 日韩欧美一区在线 | 神马午夜嘿嘿 | 日韩城人免费 | 色中色av | 欧美一级片在线 | 久久免费精品视频 | 久久激情综合 | 国产激情久久 | 一级在线视频 | 欧美jizz19性欧美 | 自拍偷在线精品自拍偷无码专区 |