Answer to Question #316204 in Python for sum of prime numbe

Question #316204

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 integers.


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



1
Expert's answer
2022-03-22T19:07:55-0400
def is_prime(num):
    k = 0
    for i in range(2, num // 2 + 1):
        if(num % i == 0):
            k += 1
    if(k <= 0):
        return True
    else:
        return False

num_str = input("input string of number: ")
num = num_str.split()
summ = 0
for i in range(len(num)):
    if(is_prime(int(num[i]))):
        summ += int(num[i])

print(summ)

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