# How to calculate sum of prime numbers from list of integers in Python?

## Question:

I am struck and need help with this..
I want to find the sum of prime numbers from a given list of integers. Here are a few test cases for the same.

``````n([3,3,1,13])

19

n([2,4,6,9,11])

13

n([-3,0,1,6])

0
``````

The Code that I have written is as follows but it fails with the test cases above..

``````def sumprimes(n):
sum1 = 0
for i in range(0,len(n)):
num = n[i]
if num > 1:
for j in range(2, int(num**0.5)+1):
if num%j != 0:
sum1 = sum1 + num
else:
sum1 = 0
return(sum1)
``````

This part is wrong:

``````        for j in range(2, int(num**0.5)+1):
if num%j != 0:
sum1 = sum1 + num
``````

you are summing `num` for each number in the range that didn’t divide.
you should sum just if all of them didn’t divide.

Simple way to do this is:

``````        prime = True
for j in range(2, int(num**0.5)+1):
if num%j == 0:
prime = False
break
if prime:
sum1 = sum1 + num
``````

Or in a more pythonic way using `all()`:

``````        if all(num%j != 0 for j in range(2, int(num**0.5)+1)):
sum1 = sum1 + num
``````

Don’t try to do everything in one function. I separated out the rest of the logic, but I’ll leave isprime to you:

``````def isprime(x):
# Replace this with your code
# separate function so it can have its own tests
return x in [3, 13, 11, 2]

def sum_prime_numbers_in_list(l):
return sum([x for x in l if isprime(x)])

if 19 != sum_prime_numbers_in_list([3, 3, 1, 13]):
raise ValueError
else:
print 'pass'

if 13 != sum_prime_numbers_in_list([2, 4, 6, 9, 11]):
raise ValueError
else:
print 'pass'

if 0 != sum_prime_numbers_in_list([-3, 0, 1, 6]):
raise ValueError
else:
print 'pass'
``````

The Answer should be this. [You should try this code, in this link.Gist]

``````def sumprimes(n):
sum1 = 0
for i in range(0,len(n)):
num = n[i]
if num > 1:
prime = True
for j in range(2, int(num**0.5)+1):
if num%j == 0:
prime = False
break
if prime:
sum1 = sum1 + num
#else:
#    sum1 = 0
return(sum1)
``````
``````> def sumprimes(n):
sum=0
fact=[]
for i in range (0,len(n)):
num=n[i]
if num>1:
fact=[]
for j in range (1,num+1):
if num%j==0:
fact=fact+[j]
if fact==[1,num]:
sum=sum+num

return(sum)
``````
``````s=input("Enter :")
arr=s.split(",")
sum=0
for f in range(2,(len(arr)+1)):
k=0
for i in range(2,f//2+1):
if (f%(int(arr[i]))==0):
k=k+1
if(k<=0):
sum=sum+f
print("Sum :",sum)
``````
``````def isPrime(n):
if (n<2):
return False
for i in range (2,n):
if (n%i==0):
return False
return True

li = []
a = [1,10,11,12,13]
for x in a:
if isPrime(x):
li.append(x)

print(li)

total = 0
for j in range(len(li)):
total += li[j]

print(total)
``````
``````def sumprimes(n):
s=0
f=[]
for i in range (0,len(n)):
num=n[i]
if num>1:
f=[]
for j in range (1,num+1):
if num%j==0:
f=f+[j]
if f==[1,num]:
s=s+num
return(s)
``````
``````lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
lst=[]
print("Prime numbers between",lower,"and",upper,"are:")

for num in range(lower,upper + 1):

if num > 1:
for i in range(2,num):
if (num % i) == 0:

break
else:
print(num)
lst.append(num)
print(lst)
sum=0
for i in lst:
sum=sum+i
print('sum of all prime numbers=%d'%sum)
``````
``````def sum_primes(a):
sum = 0
for num in a:
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
sum = sum + num
if sum < 1:
print("sum_primes(" + str(a) + ") --> " + "None")
else:
print("sum_primes(" + str(a) + ") --> " + str(sum))
sum_primes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
sum_primes([2, 3, 4, 11, 20, 50, 71])
sum_primes([])

``````
``````L = [11,14,13,12,60,50,1]

for a in L:

if(a % 2 == 0):

L.remove(a)

for a in L:

if (a % 3 == 0):

L.remove(a)

print(sum(L))
``````
``````def sumprimes(l):
def findprime(n):
if n > 1:
# check for factors
for i in range(2, n):
if (n % i) == 0:
return False
break
else:
return True
# if input number is less than
# or equal to 1, it is not prime
else:
return False
sum = 0
for x in l:
if findprime(x):
sum += x
print(sum)
``````
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.