Exercise 515

Write a python program which determines the list of prime numbers from m to n for given two integers m and n such that m < n

Solution

# create a function to test primality of given number
def primNumber(n):

# initializing the number of divisors of n
numberOfDivisors = 0
for i in range(1 , n+1):
if n%i == 0:
numberOfDivisors = numberOfDivisors + 1
if numberOfDivisors == 2:
return True
else:
return False

def listPrim(m , n):
# Initialising the searched list
listSearch = []
# the number m must be smaller than n
for i in range(m , n + 1):
if primNumber(i) == True:
listSearch.append(i)
return listSearch

# Testing algorithm
print("The searchd list for m = 1000 and n = 2000 is : " , listPrim(1000, 2000))

Younes Derfoufi
my-courses.net

Leave a Reply