Sum of prime number of list in python by R4R Team


list is l=[2,3,6,7,8,9,11,12,13]
then sum of prime number in list is 2+3+7+11+13=36


#import the functools module
import functools

print("List is ",l)

def findprime(n):
for i in range(2,int(n/2)+1):
if n%i==0:
return False
return True

#first way
for i in l:
if findprime(i):
print("Sum of prime number is :",sum)

#second way
l=filter(findprime,l )
sum=functools.reduce(lambda x,y:x+y,l)
print("Sum of prime number is :",sum)


List is [2, 3, 4, 5, 6, 7, 9, 10, 11, 11, 13, 17, 15]
Sum of prime number is : 69
Sum of prime number is : 69

-In this program, we have a list [2,3,4,5,6,7,9,10,11,11,13,17,15], then prime numbers in this list is 2,3,5,7,11,13,17 then there sum is 69.
-findprime() function is return True if the number is prime otherwise it return False.
-Filter() function filter all prime number from the list.
- reduce() function are capable to sum up the all items in the list.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!