Answer to Question #330410 in Python for Pintu

Question #330410

Sum of prime numbers in the input





1
Expert's answer
2022-04-18T17:33:14-0400
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()

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