[This page is auto-generated; please prefer to read the article’s PDF Form.]
First we observe that, there must exist integer r such that er belongs to the cycle and er equals at least one element among its next r elements (er+1,er+2,…,er+r). Specifically, any integer r satisfies this condition if and only if:
(a) r ≥ l (er belongs to the cycle) and,
(b) r ≥ n (er equals any of its next r elements).
Note that, (r ≥ l and r ≥ n) can also be written as r ≥ max(l,n).
So, for any given l and n, all integers max(l,n) onwards satisfy the criteria for r. The Brent’s algorithm attempts to find r which is the minimum power of 2, 2p (p is a non-negative integer), such that 2p ≥ max(l,n). Now onwards, we will use r to denote this specific integer 2p.
By finding r and locating er, the algorithm will have located some element in the cycle (since er belongs to the cycle), and will then proceed to find l and n.
The algorithm works as follows. It sequentially checks if the candidates r′ = 20,21,22,…, satisfy the criteria for the desired r. That is, whether er′ equals any of its next r′ elements or not. To do that, it maintains two references M (mark) and R. For each r′, M is kept at er′ while R steps through the next r′ elements comparing them with M (er′). Note that this stepping of R will finally bring it at er′+r′ = e2r′. So, for checking the next candidate r′, which is 2r′, this reference R at e2r′ can be used to set the mark M for that next candidate.
Note that a candidate r′ does not satisfy the criteria if, either (a) er′ is outside the cycle (r′ < l), or (b) er′ is inside the cycle but is distinct from its next r′ elements (r′ < n).
Following is an implementation of this algorithm in C. The above described part of this method will be referred as “Cycle-Searching” (the other part “Find l” will be discussed below). Whenever a reference, say R, is updated to f(R), we will call it one “step” taken by R.
Note that when the desired r is reached, the value of n can be found by counting the steps R has taken after er to reach the nearest element equaling er.
This algorithm finds minimum power of 2, r = 2p, such that 2p ≥ max(l,n). Since 2p is the minimum possible, we must have 2p−1 < max(l,n), which can also be written as 2p < 2 ⋅ max(l,n). So,
|
Also, this algorithm (the cycle-searching part) steps R total r + n times. So, the number of calls to f performed by it is upper-bounded by:
|
Copyright © 2021 Nitin Verma. All rights reserved.