Skip to content

Instantly share code, notes, and snippets.

@u840903
Last active January 26, 2018 13:55
Show Gist options
  • Save u840903/bd10c76ac87ba14ca0318c12bb282c24 to your computer and use it in GitHub Desktop.
Save u840903/bd10c76ac87ba14ca0318c12bb282c24 to your computer and use it in GitHub Desktop.
let triplet target =
let rec next a b =
let c = target - a - b in
match (a * a + b * b == c * c) with
| true -> a * b * c
| false when b < target / 2 -> next a (b + 1)
| false when a < target / 3 -> next (a + 1) (a + 2)
| _ -> 0 in
next 1 2
let _ = triplet 1000 (* ==> 31875000 *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment