Aaryan Khadka
Aaryan Khadka

Reputation: 23

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. 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)

Upvotes: 0

Views: 490

Answers (4)

Naman Doctor
Naman Doctor

Reputation: 182

You can write a function called get_total, which will return the sum of the prime numbers. 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.

Upvotes: 2

DirtyBit
DirtyBit

Reputation: 16782

Use a total_sum variable with initializing it to 0 and then add the prime number in the num variable into it:

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:

2
3
5
. . .

983
991
997
Sum of the Prime numbers: 76127

Upvotes: 1

Abhay Salvi
Abhay Salvi

Reputation: 1129

This can be achieved in many ways, but I am presenting you two of them:

1.) Use a list :

In my case I named it 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.

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:

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)

Upvotes: 2

whege
whege

Reputation: 1441

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()

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))

Upvotes: 1

Related Questions