Advertisement
Guest User

Untitled

a guest
Aug 17th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.63 KB | None | 0 0
  1. let cur_max = ref 0;;
  2.  
  3. let next n =
  4.     match n with
  5.     | n when (n mod 2 = 0) -> n/2
  6.     | n -> 3*n + 1
  7. in
  8.  
  9. let rec calc num steps =
  10.     if num = 1 then steps else calc (next num) (steps + 1)
  11. in
  12.  
  13. let rec check_million start =
  14.     let steps = calc start 1 in
  15.     Printf.printf "Checking %d\n" start;
  16.     if start mod 10000 = 0 then (Printf.printf "Starting at %d\n" start);
  17.     if steps > !cur_max then (
  18.         Printf.printf "New maximum value: %d for number %d\n" steps start;
  19.         flush stdout;
  20.         cur_max := steps;
  21.         ()
  22.     );
  23.     if start <= 1000000 then check_million (start + 1);
  24. in
  25. check_million 1;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement