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 40, as all the prime numbers from 18 to 40 are 19,23,23,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 1
139
def isPrime(num):
for i in range(2, num//2):
if num % i == 0:
return False
return True
m = int(input())
n = int(input())
s = 0
for i in range(m,n+1):
if isPrime(i):
s += i
print(s)
Comments
Leave a comment