Sum of prime numbers in list
Question:-
Write a function sumprimes(l) that takes as input a list of integers and returns the sum of all the prime numbers in l.
Here are some examples to show how your function should work.
>>> sumprimes([3,3,1,13])
19
>>> sumprimes([2,4,6,9,11])
13
>>> sumprimes([-3,1,6])
0
Solution:-
Write a function sumprimes(l) that takes as input a list of integers and returns the sum of all the prime numbers in l.
Here are some examples to show how your function should work.
>>> sumprimes([3,3,1,13])
19
>>> sumprimes([2,4,6,9,11])
13
>>> sumprimes([-3,1,6])
0
Solution:-
def prime(a): for j in range(2,a): if a%j==0: return False if a==1: return False if a<=0: return False return True def sumprimes(l): sum = 0 for i in range(0, len(l)): if prime(l[i]): sum = sum + l[i] return sum
Comments
Post a Comment