
Check this video out. It has a very nice explanation of this particular math problem, and with a python code example of a general solution? It is well elaborated with great details.
What I am blogging about is a methodology being applied in this example. Figuring out and separating out that common factor to reduce your size of challenge. I mentioned this in a few early posts on math challenges.
This is a shared best practice on math, on SLA and troubleshooting, on building a complex solution. It is simple and powerful.