Project Euler – Problem 75 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Hookdeck: The Serverless Event Gateway

Hookdeck is a reliable and scalable serverless event gateway for sending, receiving, authenticating, transforming, filtering, and routing events between services in your event-driven architecture.

Learn more

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^2 + b^2 = c^2 $, we can deduce that $latex c < \frac{L}{2} $; and since $latex c = m^2 + n^2 $ 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^2 + b^2 = c^2 $ then $latex ka^2 + kb^2 = kc^2 $ 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.

Whenever you’re ready, here are 4 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “Project Euler – Problem 75 Solution”

  1. Pingback: F# Weekly #2-#3, 2016 | Sergey Tihon's Blog

Leave a Comment

Your email address will not be published. Required fields are marked *