Answer to Question #80129 in Python for somnath khanra

Question #80129
Write a function sumprimes(l) that takes as input a list of integers l and retuns the sum of all the prime numbers in l.
1
Expert's answer
2018-08-27T04:43:49-0400
def isprime(n):
if (n == 0) or (n==1):
return False

elif (n == 2) or (n==3):
return True

elif (n % 2 == 0) or (n % 3 == 0):
return False

i=5
while i * i <= n:
if (n % i == 0) or (n % (i+2) == 0):
return False
i += 6

return True


def sumprimes(l):
prime_sum = 0
for elem in l:
if isprime(elem):
prime_sum += elem
return prime_sum

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS