Or maybe 3 times or 4 times etc.. The algorithm is named
after Robert W. Floyd, who was credited with its invention
by Donald Knuth.
The Floyd Algorithm finds a particular start μ
of a cycle
and the length λ
of a cycle, which are not necessarely
minimal. By the tortoise and hare algorithm, something like:
μ = k , λ = k - 1 or λ = k.
is found. So if you have μ_0
and λ_0
the minimal parameters,
they then get blown up which explains the duplicates.
Any start position shift is also a solution:
μ = μ_0 + s, λ = λ_0
And any period length multiplication is also a solution:
μ = μ_0 + s, λ = λ_0*m
So you can use CLP(FD) to find k
, example μ_0 = 13
and λ_0 = 11
:
?- [K,S,M] ins 0..1000, K #=13+S,
K #= 11*M #\/ K-1 #= 11*M, label([K,S,M]).
K = 22,
S = 9,
M = 2 .
Most of the time the duplicates are result from shift, the
above example would also have duplicates from multiplication.
The shift and multiplication explains why compare_with_stack/3
goes wrong. Thats the “ambiguity”: