Exercise 759

Write a python algorithm in the form of a function which takes as parameters two integers m and n which returns the list of all the divisors common to m and n.

Solution

def listCommonDivisors(m , n):
# initializing the list of all common divisors
lCommonDiv = []
for i in range(1 , m + 1):
if m%i == 0 and n%i == 0:
lCommonDiv.append(i)
return lCommonDiv

# Testing algorithm
print("The list of all common divisors of 18 and 48 is : " , listCommonDivisors(18 , 48))
# The output is : The list of all common divisors of 18 and 48 is : [1, 2, 3, 6]

Younes Derfoufi
my-courses.net

Leave a Reply