Skip to content

Instantly share code, notes, and snippets.

@cpdean

cpdean/tuples.ml Secret

Last active November 6, 2016 21:24
Show Gist options
  • Save cpdean/af2383855e4ed9ac4020e8db30b7eb91 to your computer and use it in GitHub Desktop.
Save cpdean/af2383855e4ed9ac4020e8db30b7eb91 to your computer and use it in GitHub Desktop.
let exchange x =
let ones = (x mod 10) in
let tens = (x - ones) / 10 in
(ones * 10) + tens;;
let is_valid_answer (grand_father_age, grand_son_age) =
let four_multiply = (grand_father_age = (grand_son_age * 4)) in
let ex_f = exchange grand_father_age in
let ex_s = exchange grand_son_age in
let three_mult = (ex_s = (ex_f * 3)) in
four_multiply && three_mult;;
let rec find (max_father, min_son) =
match (max_father, min_son) with
| (f, s) when f < (s * 4) -> (-1, -1)
| (f, s) when (is_valid_answer (f, s)) -> (f, s)
| _ -> find (max_father, min_son + 1);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment