Skip to content

Instantly share code, notes, and snippets.

@Christewart
Created December 8, 2016 02:35
Show Gist options
  • Save Christewart/571df03e7e826ab5892de834441d8b6f to your computer and use it in GitHub Desktop.
Save Christewart/571df03e7e826ab5892de834441d8b6f to your computer and use it in GitHub Desktop.
(* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
*
* Find the sum of all the multiples of 3 or 5 below 1000.
*)
let rec find_multiples counter accum =
let x = 3 * counter in
let y = 5 * counter in
if ((List.exists (fun a -> x == a) accum)) then
match (y < 1000) with
| true -> find_multiples (counter+1) ( y :: accum)
| false -> find_multiples (counter+1) accum
else
match (x < 1000, y < 1000) with
| (true, true) -> find_multiples (counter+1) (x :: y :: accum)
| (true,false) -> find_multiples (counter+1) (x :: accum)
| (false,true) -> find_multiples (counter+1) (y :: accum)
| (false,false) -> accum
;;
let result = find_multiples 1 [];;
List.map print_int result ;;
Printf.printf "\n";;
print_int (List.fold_left (+) (0) result );;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment