Project Euler – Problem 102 Solution

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.

Leave a Reply

Your email address will not be published.