Project Euler — Problem 75 Solution

Yan Cui
theburningmonk.com
Published in
2 min readJan 14, 2016

The problem description is here, and click here to see all my other Euler solutions in F#.

I based my solution on Euclid’s formula for generating Pythagorean triples.

Prob75_01_wiki

And given that max L is 1,500,000, the maximum value for m we need to consider is $latex \sqrt{\frac{L}{2}} $. Because $latex L = a + b + c $ and $latex a² + b² = c² $, we can deduce that $latex c < \frac{L}{2} $; and since $latex c = m² + n² $ we also have $latex m < \sqrt{c} $ and therefore $latex m < \sqrt{\frac{L}{2}} $.

Prob75_01

The above makes use of a recursive function to calculate the GCD (based on Euclidean Algorithm):

Prob75_04

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 $latex a² + b² = c² $ then $latex ka² + kb² = kc² $ must also be true and increment multiples of L by one.

Prob75_02

Finally, to work out the answer:

Prob75_03

This solution runs for about 350ms on my machine.

The source code for this solution is here.

--

--

Yan Cui
theburningmonk.com

AWS Serverless Hero. Follow me to learn practical tips and best practices for AWS and Serverless.