sum prime numbers in the input
given a list of integers, write a program to print the sum of all prime numbers in the list of prime numbers.
note.one is either prime nor composite number.
input
the input will be a single line containing space separated integers..
output
the output should be a single line containing the sum of all prime numbers from 1 to N
explanation
for example, if the given list of integers are
2 4 5 6 7 3 8
as 2,3,5 and 7 are prime numbers,your code should print the sum of these numbers. so the output should be 17
sample input 1
2 4 5 6 7 3 8
sample output 1
17
sample input 2
65 87 96 31 32 86 57 69 20 42
sample output 2
31
def is_prime(x):
if x < 2:
return False
if x == 2:
return True
if x%2 == 0:
return False
n = 3
while n*n <= x:
if x%n == 0:
return False
n += 2
return True
def main():
line = input()
L = [int(s) for s in line.split()]
sum = 0
for x in L:
if is_prime(x):
sum += x
print(sum)
main()
Comments
Leave a comment