Sum of prime numbers in the input
from xml.etree.ElementTree import TreeBuilder
def is_prime(x):
if x <= 1:
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():
print("Enter integer numbers (0 to exit)")
sum = 0
while True:
x = int(input())
if x == 0:
break
if is_prime(x):
sum += x
print("The sum of prime numbers is", sum)
if __name__ == '__main__':
main()
Comments
Leave a comment