Exercise 86
Write a Python algorithm that determines the list of all common divisors of two given integers m and n.
Solution
def commonDivisors(m , n):
# Initialize the list of common divisors of m and n
listCommonDiv = []
# It is the same to choose m, n or min(m,n) as loop limit
for i in range(1 , m):
if (m%i == 0 and n%i == 0):
listCommonDiv.append(i)
return listCommonDiv
# Testing algorithm
print("The list of common divisors of 36 and 24 is : " , commonDivisors(36 , 24))
# The output is : The list of common divisors of 36 and 24 is : [1, 2, 3, 4, 6, 12]
Younes Derfoufi
my-courses.net
my-courses.net