Exercise 85

Write an algorithm in Python which determines the list of all pairs of relative integers (x, y) verifying the equation : xy - x - y = n for a given integer n. give the result for n = 19.

Solution

def solutionsEq(n):
# Initialize list of solutions
listSolution = []
for x in range(-n , n + 1):
for y in range(-n , n + 1):
if x*y - x - y == 19:
listSolution.append((x,y))
return listSolution
# Testing algorithm
print("List of solutions tuples is : " , solutionsEq(19))

The output is:
List of solutions tuples is : [(-19, 0), (-9, -1), (-4, -3), (-3, -4), (-1, -9), (0, -19), (2, 21), (3, 11), (5, 6), (6, 5), (11, 3), (21, 2)]

Younes Derfoufi
my-courses.net

Leave a Reply