Answer to Question #179535 in Python for adhi chinna

Question #179535

Sum of Prime Numbers from M to N

Given two integers M and N, write a program to print the sum of prime numbers from M to N. (Both M and N are inclusive).Input


The first line of input will contain a positive integer (M).

The second line of input will contain a positive integer (N).Output


The output should be a single line containing the sum of prime numbers from M to N.Explanation


For example, if the given M and N are 5 and 11, as all the prime numbers from 5 to 11 are 5, 7, and 11. So the output should be sum of these primes (5+7+11), which is 23.


Similarly, if the given numbers are M is 18 and N is 40, as all the prime numbers from 18 to 40 are 19, 23, 29, 31,and 37. So the output should be sum of these primes (19+23+29+31+37), which is 139.

Sample Input 1

5

11

Sample Output 1

23

Sample Input 2

18

40

Sample Output 2

139




1
Expert's answer
2021-04-09T11:15:09-0400
m, n = int(input()), int(input())  # numbers m and n
sum = 0  # sup of primes
for i in range(m, n + 1):
    flag = False
    for j in range(2, i):
        if i % j == 0:
            flag = True  # composite number
    if flag == False and i != 1:  # if isn't composite number
        sum += i
print(sum)

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