{"id":150,"date":"2026-11-13T12:32:49","date_gmt":"2026-11-13T04:32:49","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-brents-cycle-search-resets-on-powers-of-two\/"},"modified":"2026-11-13T21:51:21","modified_gmt":"2026-11-13T13:51:21","slug":"how-brents-cycle-search-resets-on-powers-of-two","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-brents-cycle-search-resets-on-powers-of-two\/","title":{"rendered":"How Brent&#8217;s cycle search resets on powers of two"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Brent&#8217;s cycle algorithm<\/strong> finds a cycle in a sequence x, f(x), f(f(x)), \u2026 with constant memory. A hare walks alone. Whenever the distance since the last reset is a power of two, the tortoise jumps to the hare and the search length doubles. When the hare lands on the tortoise, the steps taken since that reset are a multiple of the cycle length, and a second walk separates the tail from the cycle. The schedule usually evaluates f fewer times than Floyd&#8217;s fixed two-speed walk.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Floyd&#8217;s tortoise and hare is correct and easy to prove, and it also evaluates f about three times per step of the tortoise. Brent asked for a schedule that spends those evaluations more carefully. The hare explores a window whose length is 1, then 2, then 4, and so on. The tortoise stands still during a window and teleports to the hare when the window is exhausted. Inside a window the algorithm stores one distance, not the whole history.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The first time the hare, moving one step at a time from the tortoise&#8217;s current post, lands on a state the tortoise holds, both pointers are on the cycle and the distance walked is a multiple of the cycle length. A second phase, the same lockstep idea Floyd uses, turns that multiple into the cycle start.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">If the sequence from the start eventually repeats, the search stops. The value \u03bb returned by the first phase is the cycle length. The second phase returns \u03bc, the length of the tail before the cycle. Time is O(\u03bc + \u03bb) function evaluations, with a smaller leading constant than Floyd&#8217;s algorithm on the usual analysis. Extra memory is a constant number of states and counters.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The sequence still has to be a function: one successor per state. A directed graph with branching is a depth-first search. A terminal state with no successor has to be defined, or the hare cannot move. Brent&#8217;s schedule does not change Pollard&#8217;s rho by itself. Rho can use either cycle finder around its polynomial walk.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set the tortoise to the start, the hare to f of the start, the current power to 1, and the steps in this window to 1.<\/li>\n<li>While the tortoise and the hare differ, if the window is full then move the tortoise to the hare, double the power, and clear the window count. Then advance the hare once and increment the window count.<\/li>\n<li>When they meet, the window count is the cycle length \u03bb.<\/li>\n<li>Put both pointers at the start. Advance one of them \u03bb steps. Then advance both together until they meet. The number of lockstep steps is the tail length \u03bc, and the meeting state is the cycle entrance.<\/li>\n<li>Store each state you still need. The algorithm keeps only the two pointers, so a caller who wants the whole cycle walks \u03bb steps from the entrance.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Constant memory belongs to a single successor function. Floyd&#8217;s algorithm is the same problem with a different schedule. A general graph cycle is a back edge in a depth-first search. Brent&#8217;s search is the power-of-two cycle finder.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the window count is reported as the tail<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Treat the first meeting distance as the index of the entrance<\/td>\n<td>That distance is the cycle length. The entrance comes from the second walk, which advances one pointer \u03bb steps ahead of the other.<\/td>\n<\/tr>\n<tr>\n<td>Double the window without moving the tortoise<\/td>\n<td>The hare then compares against a stale state that may lie off the cycle. The reset places the tortoise on the hare&#8217;s current state before the longer search.<\/td>\n<\/tr>\n<tr>\n<td>Store every state and call the extra memory constant<\/td>\n<td>A set of seen states detects a repeat and uses memory proportional to \u03bc + \u03bb. Brent&#8217;s bound is a handful of variables.<\/td>\n<\/tr>\n<tr>\n<td>Run the same two pointers on a node with several outgoing edges<\/td>\n<td>The next state is no longer a function. The search does not choose which edge to follow.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A fixed two-speed walk is Floyd&#8217;s algorithm. A factored integer from a polynomial iteration is Pollard&#8217;s rho. Brent&#8217;s algorithm is the power-of-two cycle search.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment lets the hare walk alone and doubles the window whenever the window fills, moving the tortoise up to the hare at each reset. On the same rho shape as Floyd&#8217;s example it prints tail <code>3<\/code> and cycle <code>3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">succ = [1, 2, 3, 4, 5, 3]\ndef f(x):\n    return succ[x]\n\npower = lam = 1\ntortoise, hare = 0, f(0)\nwhile tortoise != hare:\n    if power == lam:\n        tortoise = hare\n        power *= 2\n        lam = 0\n    hare = f(hare)\n    lam += 1\ntortoise = hare = 0\nfor _ in range(lam):\n    hare = f(hare)\nmu = 0\nwhile tortoise != hare:\n    tortoise, hare = f(tortoise), f(hare)\n    mu += 1\nassert (lam, mu) == (3, 3)\nprint(mu, lam)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Brent&#8217;s cycle finder is the restart used inside practical <strong>Pollard rho<\/strong> factoring: the tortoise waits at powers of two while the hare walks. Floyd&#8217;s paired walk is the older cycle test. Either one is there to find the loop, not to sort.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Brent&#8217;s schedule when a shorter proof matters more than the evaluation count. Floyd&#8217;s walk is enough. Keep Brent when each evaluation of f is expensive and the sequence is still a function.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Brent&#8217;s cycle algorithm returns the power-of-two reset and the second walk that separates the tail from the cycle. <strong>Brent&#8217;s algorithm finds a cycle length by walking a hare across doubling windows and finds the entrance with a second lockstep walk, using constant memory.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Brent&#8217;s cycle algorithm finds a cycle in a sequence x, f(x), f(f(x)), \u2026 with constant memory. A hare walks alone. Whenever the distance since the last reset is a power of two, the tortoise jumps to the hare and the search length doubles. When the hare lands&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,44],"tags":[],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sequences"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions\/549"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}