loops - Finding the sum of prime Numbers in a List in Python -


i trying question exercise stuck.i try precise possible

i find sum of primes list input

say input given function list like=[17,51,29,39],so should return 46 answer 17+29=46

here's code write:

def sumprimes(l):     sum=0     value in l:         in range(1,float((value)/2)):             if value%i==0:                       sum+=value          print(sum)  

note-on passing list,the program should work. haven't written part.

thanks in advance

you'd better create function test if number prime @wim said.

def is_prime(n):     if n < 2:         return false     elif n <= 3:         return true     elif n % 2 == 0:         return false      # round sqrt of n     length = int(n**.5) + 1  # n**.5 equal sqrt(n)     in range(3, length, 2):         if n % == 0:             return false      return true 

this simple , efficient primality test. can sum with:

def sum_primes(l):     return sum(n n in l if is_prime(n)) 

Comments