Project Euler — Problem 102 Solution

The prob­lem descrip­tion is here, and click here to see all my oth­er Euler solu­tions in F#.

 

After read­ing the ques­tion, a quick search on how to test if a point is in a tri­an­gle turned up this use­ful SO answer. Trans­lat­ing the algo­rithm to F# is pret­ty triv­ial:

Prob102_01

I saved the triangle.txt file along­side the script file, so to make it eas­i­er to load and parse. The con­tent 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) coor­di­nates sep­a­rat­ed by com­ma.

F# 4 also added a chunkBy­Size com­bi­na­tor to the Array mod­ule, which comes in handy here:

Prob102_02

This solu­tion runs for 20ms on my machine.

 

The source code for this solu­tion is here.