{"id":235,"date":"2026-11-13T12:35:27","date_gmt":"2026-11-13T04:35:27","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-pollards-rho-hunts-a-factor-on-a-cycle\/"},"modified":"2026-11-13T21:53:23","modified_gmt":"2026-11-13T13:53:23","slug":"how-pollards-rho-hunts-a-factor-on-a-cycle","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-pollards-rho-hunts-a-factor-on-a-cycle\/","title":{"rendered":"How Pollard&#8217;s rho hunts a factor on a cycle"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Pollard&#8217;s rho<\/strong> looks for a nontrivial factor of a composite n by iterating a polynomial, usually x\u00b2 + c, modulo n, and taking gcd of n with the difference of two values on the sequence. By the birthday paradox the sequence cycles quickly modulo a secret prime factor p, and the gcd sees p when two iterates are congruent modulo p and not modulo n. With Floyd&#8217;s cycle finding the expected time is about <strong>O(n^{1\/4})<\/strong> arithmetic operations. The method is Monte Carlo and can fail for an unlucky c.<\/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\">Trial division up to sqrt(n) is exact and hopeless when the smallest prime factor is large. A random map on the residues modulo p collides in about sqrt(p) steps. The algorithm cannot see the residues modulo p, because p is the unknown. It iterates modulo n and asks the gcd to reveal the collision. That is the whole trick.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The name rho is the shape of the sequence: a tail into a cycle. Floyd&#8217;s tortoise and hare find the cycle without storing every iterate. Brent&#8217;s cycle algorithm is a common replacement with fewer evaluations.<\/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 gcd returns d with 1 &lt; d &lt; n, then d is a nontrivial factor. If it returns n, that c and start point failed and another c is tried. If it returns 1, the walk continues. The expected cost to find the smallest prime factor p is O(n^{1\/4}) up to logarithmic factors from arithmetic, because sqrt(p) \u2264 n^{1\/4}. Extra memory is a few integers. The algorithm does not always terminate on a product of two close large primes within a short budget. It is a practical factoriser for moderate factors, not a proof of the difficulty of RSA moduli.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A prime input never yields a nontrivial factor. Miller-Rabin should screen the input. The polynomial x\u00b2 + c must not be so structured that the map is not mixing. c = 0 and c = \u22122 are the classical bad choices.<\/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>If n is even, return 2. If a primality test says n is prime, stop. There is no nontrivial factor.<\/li>\n<li>Choose c, often 1, and a start x. Set the tortoise and the hare to x.<\/li>\n<li>Repeat: move the tortoise one step and the hare two steps under the map z \u2192 z\u00b2 + c modulo n. Compute g = gcd(|hare \u2212 tortoise|, n).<\/li>\n<li>If g is 1, continue. If g is n, restart with a different c or start. If g is anything else, return g.<\/li>\n<li>Optionally use Brent&#8217;s cycle detection in place of Floyd&#8217;s to reduce the number of polynomial evaluations. The gcd test is the same.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The n^{1\/4} figure is an expectation for the smallest factor, not a worst-case theorem and not a factorisation of a balanced RSA modulus in reasonable time. The gcd can return n, which is a failed run, not a factor. Deterministic factorisation of a small integer can just use trial division or the linear sieve. Rho is for one integer whose small factor is unknown.<\/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 a gcd of n is treated as a factor<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Return n when the gcd equals n<\/td>\n<td>Every difference was divisible by n. The run found the trivial factor. Change c and restart.<\/td>\n<\/tr>\n<tr>\n<td>Store every iterate to find the cycle<\/td>\n<td>The result can be correct and the memory is the length of the rho, which is what Floyd&#8217;s two pointers avoid.<\/td>\n<\/tr>\n<tr>\n<td>Use c = 0<\/td>\n<td>The map x\u00b2 modulo n is a poor pseudorandom walk for this method. Choose a small nonzero c other than the known degenerate cases.<\/td>\n<\/tr>\n<tr>\n<td>Quote the running time as O(n^{1\/4}) worst case<\/td>\n<td>The birthday argument is expected time for a random map. An unlucky polynomial or a prime input does not obey a deterministic clock.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Primality, before any attempt to factor, is Miller-Rabin. Bulk factorisation of every integer up to n is the linear sieve. Pollard&#8217;s rho is the birthday-paradox factoriser for one composite.<\/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 walks x and x\u00b2+1 modulo n with a tortoise and a hare, and takes a gcd along the way. For 91 the gcd is a nontrivial factor, <code>7<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math\nn, c = 91, 1\ntortoise = hare = 2\nfactor = 1\nwhile factor == 1:\n    tortoise = (tortoise * tortoise + c) % n\n    hare = (hare * hare + c) % n\n    hare = (hare * hare + c) % n\n    factor = math.gcd(abs(hare - tortoise), n)\nassert factor not in (1, n) and n % factor == 0\nprint(factor)\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\"><strong>Pollard&#8217;s rho<\/strong> finds a small prime factor of a composite, and it is one of the methods inside GMP&#8217;s factoring. A balanced RSA modulus is not attacked by this walk; the method is for integers that have a small factor. The elliptic-curve method is the other factoring tool in that toolbox.<\/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 rho when n is prime or when trial division up to a small bound already found the factor. Keep it to peel a moderate prime factor from a large composite, and restart when the gcd comes back equal to n.<\/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 Pollard&#8217;s rho returns the polynomial walk, the gcd with the tortoise-hare difference, and the expected n to the one-fourth cost. <strong>Pollard&#8217;s rho finds a nontrivial factor by walking x\u00b2 + c modulo n and taking a gcd when Floyd&#8217;s cycle pointers meet, in expected time about n to the one-fourth.<\/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: Pollard&#8217;s rho looks for a nontrivial factor of a composite n by iterating a polynomial, usually x\u00b2 + c, modulo n, and taking gcd of n with the difference of two values on the sequence. By the birthday paradox the sequence cycles quickly modulo a secret prime&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,42],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-number-theory"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/235","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=235"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}