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

python素數之和

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

問題描述

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

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 

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

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

這是我用來生成素數列表的代碼.

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 變量在您的外部循環的每次迭代中被重置.將初始化移出該循環.

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

此外,a == 2 檢查應該只在外循環的每次迭代中發生一次.將其移出內循環.

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 

結果:

1060

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

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 循環比增加計數器的 while 循環更符合規范:

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

如果您覺得大膽,可以使用列表推導來減少您使用的循環數量:

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

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

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 老牛影视av一区二区在线观看 | 91香蕉| 久久免费看 | 久草视频在线播放 | 精品无码三级在线观看视频 | 欧美午夜在线 | 日韩在线免费视频 | 999久久久久久久 | 91精品国产一二三 | 久久不卡日韩美女 | 国产精品久久久av | 国产美女在线免费观看 | 全部免费毛片在线播放网站 | 99国内精品 | 久久99网站 | 日韩精品一区二区三区中文字幕 | 免费黄色a视频 | 国产精品免费一区二区三区 | 91精品国产欧美一区二区成人 | 嫩草视频入口 | 黄网站在线播放 | 青青草原综合久久大伊人精品 | 日日骚视频 | 夜夜骑av| а天堂中文最新一区二区三区 | 国产超碰人人爽人人做人人爱 | 一区精品视频在线观看 | 久久99精品久久久久久 | www性色| 福利一区二区 | 97国产一区二区精品久久呦 | 欧美视频| 色综合视频 | 高清久久久 | 亚洲精品一二三 | 日韩在线三级 | 蜜桃综合在线 | 国产极品车模吞精高潮呻吟 | 天天综合网91| 色伊人网 | 一二区成人影院电影网 |