python质数总和

编程入门 行业动态 更新时间:2024-10-25 02:21:37
本文介绍了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质数总和

本文发布于:2023-11-29 23:05:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:质数   总和   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!