Sum of prime numbers from m to n
Input
5 11
def isPrime(num):
# define a flag variable
flag = True
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = False
# break out of loop
break
return flag
# To take input from the user
n = int(input("Enter n: "))
m = int(input("Enter m: "))
sum = 0
while n <= m:
if isPrime(n):
sum += n;
n = n + 1
print(sum)
Comments
Leave a comment