[英]How do I add all of these prime numbers?
I am using python and trying to make a function in which it will generate all PRIME numbers from 1 to 1000, and then add up all of the prime numbers.我正在使用 python 并尝试制作一个 function,它将生成从 1 到 1000 的所有素数,然后将所有素数相加。 So far, I have made it to where all of the prime numbers are printed, but I am confused on how to add them up.
到目前为止,我已经找到了所有素数的打印位置,但我对如何将它们相加感到困惑。 Here is my code:
这是我的代码:
lower = 0
upper = 1000
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
This can be achieved in many ways, but I am presenting you two of them:这可以通过多种方式实现,但我向您介绍其中两种:
1.) Use a list : 1.) 使用列表:
In my case I named it The_list()
.就我而言,我将其命名为
The_list()
。 It stores all values if a prime number.如果是素数,它会存储所有值。 For that I used
.append()
function. And then sum up all the values stored using sum()
function.为此,我使用了
.append()
function。然后使用sum()
function 对存储的所有值求和。
lower = 0
upper = 1000
The_list = []
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
The_list.append(num)
print(sum(The_list))
2.) Use a variable instead : 2.)改用变量:
lower = 0
upper = 1000
total = 0
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
total = total + num
print(total)
You can write a function called get_total, which will return the sum of the prime numbers.您可以编写一个名为 get_total 的 function,它将返回质数之和。 This can be done as following:
这可以通过以下方式完成:
def get_total(lower, upper):
total = 0
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
total += num
return total
total_value = get_total(0,1000)
print(total_value)
By doing this, you can reuse this function and write any values for the lower and upper numbers.通过这样做,您可以重复使用这个 function 并为下限和上限写入任何值。
Use a total_sum
variable with initializing it to 0
and then add the prime number in the num
variable into it:使用
total_sum
变量并将其初始化为0
,然后将num
变量中的质数添加到其中:
lower = 0
upper = 1000
total_sum = 0
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
total_sum += num
print("Sum of the Prime numbers: {}".format(total_sum))
OUTPUT: OUTPUT:
2
2个
33个
55个
.. .
. .
.
983
983
991991
997997
Sum of the Prime numbers: 76127质数之和:76127
Just create an empty list before the loop, then when you determine which numbers are prime, append them to the list, and at the end call sum()
只需在循环之前创建一个空列表,然后当您确定哪些数字是质数时,将它们 append 添加到列表中,最后调用
sum()
lower = 0
upper = 1000
primes=[]
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
mods = [(num % i == 0) for i in range(2, num-1)]
if any(mods):
break
else:
primes.append(num)
else:
continue
print(sum(primes))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.