ocaml: phase 5.1 hailstone.ml baseline (Collatz length from 27 = 111 steps)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Iterative Collatz / hailstone sequence:
let collatz_length n =
let m = ref n in
let count = ref 0 in
while !m > 1 do
if !m mod 2 = 0 then m := !m / 2
else m := 3 * !m + 1;
count := !count + 1
done;
!count
27 is the famous 'long-running' Collatz starter. Reaches a peak of
9232 mid-sequence and takes 111 steps to bottom out at 1.
64 baseline programs total.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
"frequency.ml": 5,
|
||||
"gcd_lcm.ml": 60,
|
||||
"grep_count.ml": 3,
|
||||
"hailstone.ml": 111,
|
||||
"hanoi.ml": 1023,
|
||||
"fizzbuzz.ml": 57,
|
||||
"flatten_tree.ml": 28,
|
||||
|
||||
13
lib/ocaml/baseline/hailstone.ml
Normal file
13
lib/ocaml/baseline/hailstone.ml
Normal file
@@ -0,0 +1,13 @@
|
||||
let collatz_length n =
|
||||
let m = ref n in
|
||||
let count = ref 0 in
|
||||
while !m > 1 do
|
||||
if !m mod 2 = 0 then m := !m / 2
|
||||
else m := 3 * !m + 1;
|
||||
count := !count + 1
|
||||
done;
|
||||
!count
|
||||
|
||||
;;
|
||||
|
||||
collatz_length 27
|
||||
Reference in New Issue
Block a user