Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading