Exercise 84
write a python algorithm as a function which takes as argument an integer n and which returns the number of tuples (u, v, w) of integers such that u, v and w are all divisors of n and n = u + v + w. Example for n = 18 the list of sought tuples is: L = [(3, 6, 9), (3, 9, 6), (6, 3, 9), (6, 6, 6), (6, 9, 3), (9, 3, 6), ( 9, 6, 3)].
Solution
def numberOfTuples(n):
# Initializing the sought list
listTuples = []
for i in range(1,n+1):
for j in range(1,n+1):
for k in range(1,n+1):
if i + j + k == n and n%i == 0 and n%j == 0 and n%k == 0:
listTuples.append((i , j , k))
return listTuples
# Testing algorithm
print("List of tuples searched for n = 18 is : " , numberOfTuples(18))
Younes Derfoufi
my-courses.net
my-courses.net