Skip to content

Instantly share code, notes, and snippets.

@ldunn
Created September 21, 2010 00:08
Show Gist options
  • Select an option

  • Save ldunn/588905 to your computer and use it in GitHub Desktop.

Select an option

Save ldunn/588905 to your computer and use it in GitHub Desktop.
(* Project Euler Problem 9
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
*)
let getTriplet a b = [a;b;sqrt ((a*a) + (b*b))]
let euler9 =
let ans = ref 0.0
let triplets = ref []
for i in [1.0..1000.0] do
for j in [1.0..1000.0] do
triplets := (getTriplet i j) :: !triplets
for triplet in !triplets do
if (List.reduce (+) triplet) = 1000.0 then
ans := List.reduce (*) triplet
!ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment