Project Euler – Problem 102 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

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

 

After reading the question, a quick search on how to test if a point is in a triangle turned up this useful SO answer. Translating the algorithm to F# is pretty trivial:

Prob102_01

I saved the triangle.txt file alongside the script file, so to make it easier to load and parse. The content of the file looks like this:

-340,495,-153,-910,835,-947

-175,41,-421,-714,574,-645

-547,712,-352,579,951,-786

419,-864,-83,650,-399,171

so for each line, we have the 3 (x,y) coordinates separated by comma.

F# 4 also added a chunkBySize combinator to the Array module, which comes in handy here:

Prob102_02

This solution runs for 20ms on my machine.

 

The source code for this solution is here.

Whenever you’re ready, here are 3 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 to level up your serverless skills quickly.
  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.

Leave a Comment

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