haar_lib/algo/
find_cycle.rs1#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
12pub struct Rho {
13 pub tail: u64,
15 pub cycle: u64,
17}
18
19pub fn find_cycle<T>(init: T, f: impl Fn(T) -> T) -> Rho
23where
24 T: Copy + Eq,
25{
26 let mut a = init;
27 let mut b = init;
28 loop {
29 a = f(a);
30 b = f(f(b));
31
32 if a == b {
33 break;
34 }
35 }
36
37 let mut tail = 0;
38 let mut c = init;
39 while a != c {
40 c = f(c);
41 a = f(a);
42 tail += 1;
43 }
44
45 let mut cycle = 0;
46 loop {
47 a = f(a);
48 c = f(f(c));
49 cycle += 1;
50
51 if a == c {
52 break;
53 }
54 }
55
56 Rho { tail, cycle }
57}