I based my solution on Euclid’s formula for generating Pythagorean triples.
And given that max L is 1,500,000, the maximum value for m we need to consider is . Because and , we can deduce that ; and since we also have and therefore .
The above makes use of a recursive function to calculate the GCD (based on Euclidean Algorithm):
For efficiency, we’ll create a cache to store the number of ways L can be used to create integer sided right-angle triangle. As we iterate through the m and n pairs we generated above, we’ll take advantage of the fact that if then must also be true and increment multiples of L by one.
Finally, to work out the answer:
This solution runs for about 350ms on my machine.
The source code for this solution is here.
Subscribe to my newsletter and get new contents delivered straight to your inbox :-)