ocaml: phase 5.1 floyd_cycle.ml baseline (tortoise-hare, mu=0 lam=8 -> 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Floyd's cycle detection on a numeric function f(x) = (2x + 5) mod 17.
Three phases:
Phase 1: advance slow/fast until collision inside the cycle
(fast double-steps, slow single-steps)
Phase 2: restart slow from x0; advance both by 1 until they
meet — count is mu (length of tail before cycle)
Phase 3: advance fast around cycle once until it meets slow
— count is lam (cycle length)
For x0 = 1, the orbit visits 1, 7, 2, 9, 6, 0, 5, 15 then returns
to 1 — pure cycle of length 8, mu = 0, lam = 8. Encoded as
mu*100 + lam = 8.
Tests three sequential while loops sharing ref state,
double-step `fast := f (f !fast)`, meeting-condition flag.
185 baseline programs total.
This commit is contained in:
@@ -85,6 +85,7 @@
|
||||
"fizzbuzz.ml": 57,
|
||||
"flatten_tree.ml": 28,
|
||||
"flood_fill.ml": 7,
|
||||
"floyd_cycle.ml": 8,
|
||||
"floyd_warshall.ml": 9,
|
||||
"lis.ml": 6,
|
||||
"list_ops.ml": 30,
|
||||
|
||||
29
lib/ocaml/baseline/floyd_cycle.ml
Normal file
29
lib/ocaml/baseline/floyd_cycle.ml
Normal file
@@ -0,0 +1,29 @@
|
||||
let f x = (x * 2 + 5) mod 17
|
||||
|
||||
let floyd_cycle x0 =
|
||||
let slow = ref x0 in
|
||||
let fast = ref x0 in
|
||||
let meet = ref false in
|
||||
while not !meet do
|
||||
slow := f !slow;
|
||||
fast := f (f !fast);
|
||||
if !slow = !fast then meet := true
|
||||
done;
|
||||
slow := x0;
|
||||
let mu = ref 0 in
|
||||
while !slow <> !fast do
|
||||
slow := f !slow;
|
||||
fast := f !fast;
|
||||
mu := !mu + 1
|
||||
done;
|
||||
let lam = ref 1 in
|
||||
fast := f !slow;
|
||||
while !slow <> !fast do
|
||||
fast := f !fast;
|
||||
lam := !lam + 1
|
||||
done;
|
||||
!mu * 100 + !lam
|
||||
|
||||
;;
|
||||
|
||||
floyd_cycle 1
|
||||
Reference in New Issue
Block a user