## Thursday, September 17, 2020

Solution Exercise 755: python algorithm to find the list of all prim number contained in given intervall

#### Exercise 755

Write a Python algorithm in the form of a function which takes as argument an integer n and which returns the list of all the prime integers contained in the interval [n, 2n] Example for n = 9, the function returns the list of the all prim numbers contained in the interval [9, 18] which is L = [11, 13, 17].

#### Solution

``````# creating function to test if given number is prim or not
def testPrim(m):
# Initializing number of divisors
numberDivisors = 0

# while i is divisor of m we increment numberDivisors
for i in range(1 , m + 1):
if(m%i == 0):
numberDivisors = numberDivisors + 1

# a number is prime if and only if it has two divisors, 1 and itself
if (numberDivisors == 2):
return True
else:
return False

def listPrim(n):
# initializing the list of prim numbers
L = []
for i in range(n , 2*n):
if testPrim(i):
L.append(i)
return L
# Testing algorithm
print("The list of all prim numbers in [9 , 18] is : " , listPrim(9))``````

Younes Derfoufi
my-courses.net