File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < tuple>
2+
3+ using namespace std ;
4+
5+ template <typename T>
6+ tuple<int , int > brent (T const & v) {
7+ int power = 1 ;
8+ int lam = 1 ;
9+ auto tortoise = v, hare = next (v);
10+
11+ while (tortoise != hare) {
12+ if (power == lam) {
13+ tortoise = hare;
14+ power *= 2 ;
15+ lam = 0 ;
16+ }
17+ hare = next (hare);
18+ lam++;
19+ }
20+
21+ int it = 0 ;
22+ tortoise = hare = v;
23+ for (int i = 0 ; i < lam; i++) {
24+ hare = next (hare);
25+ }
26+ // now hare has walked lambda distance
27+
28+ int mu = 0 ;
29+ tortoise = v;
30+ while (tortoise != hare) {
31+ tortoise = next (tortoise);
32+ hare = next (hare);
33+ mu++;
34+ }
35+ // hare has walked lambda+mu and
36+ // tortoise has walked mu
37+ // they meet at start of cycle
38+
39+ // lenght outside of cycle and cycle lenght
40+ return {mu, lam};
41+ }
You can’t perform that action at this time.
0 commit comments