[This page is auto-generated; please prefer to read the article’s PDF Form.]
We initialize two references R0 and Rn at e0 and en respectively. Rn can be placed at en by stepping n times from e0. Now, after l steps, R0 will be at index l and Rn will be at index (due to equation (1) in [1]):
|
So, to find l, we can iteratively step R0 and Rn till they meet, while counting the steps. This step count will be l.
Alternatively, we can use another more efficient approach. When the cycle-searching loop terminates, M is at er. We will first place M at ej, where j is a multiple of n. If n∣r, we are done with j = r. Otherwise, we use the fact that (r − r mod n), and hence (r + n − r mod n), is always a multiple of n. So, we step M (n − r mod n) times to bring it at ej with j = (r + n − r mod n).
Now, initialize a reference R0 at e0. After l steps, it will be at index l. Also, M, which is at ej, after l steps will be at index (due to equation (1) in [1]):
|
So, to find l, we can iteratively step R0 and M till they meet, while counting the steps. This step count will be l.
The “Find l” part in method brent() implements this approach.
The earlier approach steps R0 and Rn l and n + l times respectively. This approach steps R0 l times, but steps M l or (n − r mod n) + l times (based on n∣r or not). Note that, (n − r mod n) < n when n ∤ r.
■
[1] Nitin Verma. Cycle Detection: Floyd’s Algorithm.
https://mathsanew.com/articles/cycle_detection.pdf (2021).
[2] R. P. Brent. An Improved Monte Carlo Factorization Algorithm. BIT
Numer. Math., Vol 20 (1980), 176–184.
https://maths-people.anu.edu.au/~brent/pd/rpb051i.pdf.
Copyright © 2021 Nitin Verma. All rights reserved.