简体   繁体   English

我如何添加所有这些素数?

[英]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个
3 3个
5 5个
. . . . . .

983 983
991 991
997 997
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.

相关问题 如何找到一定范围内的所有素数并打印出来? - How do i find all the prime numbers in a certain range and print them out? Python:如何将除数分类为合数和素数? - Python: How do I classify divisors into composite and prime numbers? 我如何过滤从 1 到 100(都包括在内)的素数和以 7 结尾的数字的列表? - How do i filter a list of 1 to 100 (both included) for prime numbers and numbers that end with 7? 查找小于或等于用户使用循环输入的数字的素数。 我怎么做? - Find prime numbers less than or equal to the number the user has entered with loops. How do I do that? 如何编写一个程序来显示此数字列表中按最大质数排序的值? - How do I write a program that displays the values in this list of numbers sorted by their largest prime factor? 我的素数程序中的指数引发了内存错误,我该如何解决? - An exponent within my prime numbers program throws a memory error, how do I solve this? 如何在Python 3.5中找到给定范围内的质数之和? - How do I find the sum of prime numbers in a given range in Python 3.5? 我想要一个小于 integer 的所有质数的列表 - I want a list of all the prime numbers that are smaller than a integer 范围内的所有素数 - All prime numbers within a range 大学问题,我不知道该怎么办(质数) - College problem and I have no idea what to do (Prime numbers)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM