Exercise 513

Create a python algorithm which determines the list of tuples of digits (n, m), such as: the integer x=142nm is divisible by 7. Example for (m, n) = (0, 7), the number 1407 is divisible by 7 .

Solution


x = 1400
# Initializing list of tuples solutions
L = []

# Browsing through all integers between 0 and 9, to find tuples solution
for m in range(0, 10):
for n in range (0, 10):

# We test if the number 14mn is divisible by 7 and we append the tuple (m,n) to the list L
if( x + 10*m + n)%7 == 0:
L.append((n,m))
print("The list of tuples found is : " , L)
# The output is: The list of tuples found is : [(0, 0), (7, 0), (4, 1), (1, 2), (8, 2), (5, 3), (2, 4), (9, 4), (6, 5), (3, 6), (0, 7), (7, 7), (4, 8), (1, 9), (8, 9)]


Younes Derfoufi
my-courses.net

Leave a Reply