Finding all possible solution pairs to an algebra problem with constraints in python


I am trying to use python to create list of all pairs of numbers between 2 and 75 that average to 54, where each value in each solution pair can go up to 6 decimal places. And so I want to have a list of all of these solution pairs.

I have tried using solve() from SymPy to solve this as an algebra problem, but I am unsure how to handle this when the equation has many solutions, and further includes constraints. How can I approach this problem so that I account for my constraints, where all values in each solution pair need to be between 2 and 75?


You may observe that all the solutions are values (n,m) where 33<=n<=75 and n+m=108.

Hence given n, we may determine m, by just subtracting it from 108.

We iterate through the values of n and produce the values of m, using a generator, since the list is much too big to store in memory.

Note that there are 42 million solution pairs.

To display them, you can iterate through them

decimals = 3

m = 10**decimals

solutions = ((i/m,round(108-i/m,decimals)) for i in range(33*m,75*m+1))

for solution_pair in solutions:
  a, b = solution_pair
  print(a, b)
Answered By: Mous
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.