{"id":283,"date":"2026-11-13T12:36:53","date_gmt":"2026-11-13T04:36:53","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-lucas-lehmer-test-checks-a-mersenne-number\/"},"modified":"2026-11-13T21:58:30","modified_gmt":"2026-11-13T13:58:30","slug":"how-the-lucas-lehmer-test-checks-a-mersenne-number","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-lucas-lehmer-test-checks-a-mersenne-number\/","title":{"rendered":"How the Lucas-Lehmer test checks a Mersenne number"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Lucas-Lehmer test<\/strong> decides whether a Mersenne number M = 2^p \u2212 1 is prime, for an odd prime p. It sets s = 4 and repeats s = s\u00b2 \u2212 2, modulo M, p\u22122 times. M is prime exactly when the final s is 0 modulo M. The test is deterministic. Each step is a squaring of a p-bit number, so the cost is <strong>O(p)<\/strong> squarings, which is practical for the exponents used in the record Mersenne searches and irrelevant to a general integer.<\/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\">A Mersenne number has a rigid shape, one less than a power of two. Most primality tests ignore that shape. Lucas-Lehmer uses it. The sequence s_{i+1} = s_i\u00b2 \u2212 2 is the trace of a recurring squaring in the ring Z[\u221a3], and the theorem says the (p\u22122)-th term is divisible by M exactly when M is prime, provided p itself is prime.<\/p>\n\n\n<p class=\"wp-block-paragraph\">If p is composite, 2^p \u2212 1 is also composite, with a factorisation that follows the factors of p. The test is never asked that question. The caller checks that p is prime first, which for the size of p in a Mersenne search is a much smaller problem.<\/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\">For odd prime p, the test returns a correct prime-or-composite answer for 2^p \u2212 1. The number of loop iterations is exactly p\u22122. Implementing one fewer or one more iteration tests a different statement and is wrong. Modular reduction modulo 2^p \u2212 1 is a shift-and-add, because 2^p \u2261 1, which is why these numbers are friendlier than a random integer of the same size.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The test says nothing about a general odd integer. A number that is merely close to a power of two does not inherit the theorem. Miller-Rabin is the general test. Lucas-Lehmer is the Mersenne test.<\/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>Confirm that p is an odd prime. If it is not, do not run the test. If p is composite, 2^p \u2212 1 is composite for a different reason.<\/li>\n<li>Set M = 2^p \u2212 1 and s = 4.<\/li>\n<li>Repeat p\u22122 times: replace s by (s\u00b2 \u2212 2) mod M. The modulo uses the shift reduction for this particular M.<\/li>\n<li>If the final s is 0, M is prime. Otherwise M is composite.<\/li>\n<li>Do not stop early because s became 0 in the middle of the loop. The theorem looks at the final term.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The theorem is for Mersenne numbers with prime exponents. It is not a general-purpose Lucas test, of which there are several with different parameters, and it is not a factorisation. A composite result does not name a factor. The Great Internet Mersenne Prime Search uses this test, with an irrational-base check of the last digits, on exponents that have already passed trial factorisation.<\/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 loop count is p instead of p minus 2<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Iterate p times from s = 4<\/td>\n<td>The theorem&#8217;s index is p\u22122. An off-by-one accepts composites or rejects primes.<\/td>\n<\/tr>\n<tr>\n<td>Run the test for a composite exponent<\/td>\n<td>The hypothesis is an odd prime exponent. A composite exponent makes 2^p \u2212 1 composite, and the sequence test is not the argument.<\/td>\n<\/tr>\n<tr>\n<td>Use the same recurrence for a general integer n<\/td>\n<td>The modular ring and the theorem are about 2^p \u2212 1. A general n needs Miller-Rabin or another general test.<\/td>\n<\/tr>\n<tr>\n<td>Treat a final zero as a factorisation<\/td>\n<td>Zero means M divides the final term, which the theorem converts into primality. It is not a factor.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">General primality is Miller-Rabin. A deterministic polynomial-time test for arbitrary integers is AKS. Lucas-Lehmer is the Mersenne primality test.<\/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 starts at s = 4 and iterates s = s\u00b2 \u2212 2 modulo 2^p \u2212 1, exactly p\u22122 times. For p = 5 the Mersenne number is 31 and the final residue is <code>0<\/code>, so 31 is prime.<\/p>\n\n<pre><code class=\"language-python\">def lucas_lehmer(p):\n    mersenne = (1 &lt;&lt; p) - 1\n    s = 4\n    for _ in range(p - 2):\n        s = (s * s - 2) % mersenne\n    return s == 0\n\nassert lucas_lehmer(5) and lucas_lehmer(7) and not lucas_lehmer(11)\nprint(0 if lucas_lehmer(5) else 1)\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\">The <strong>Great Internet Mersenne Prime Search<\/strong> uses the Lucas-Lehmer test on Mersenne numbers 2^p \u2212 1. The test is a squaring loop, not a general primality test. A general integer goes to Miller-Rabin.<\/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 Lucas-Lehmer for any integer that is not one less than a power of two with prime exponent. Keep it for exactly those numbers, and iterate p\u22122 times.<\/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 the Lucas-Lehmer test returns the recurrence s\u00b2 \u2212 2 and the p\u22122 iterations that decide a Mersenne prime. <strong>The Lucas-Lehmer test proves a Mersenne number 2^p \u2212 1 prime or composite by iterating s = s\u00b2 \u2212 2 modulo that number, exactly p\u22122 times, and checking for zero.<\/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: The Lucas-Lehmer test decides whether a Mersenne number M = 2^p \u2212 1 is prime, for an odd prime p. It sets s = 4 and repeats s = s\u00b2 \u2212 2, modulo M, p\u22122 times. M is prime exactly when the final s is 0 modulo&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-283","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\/283","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=283"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":682,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/283\/revisions\/682"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}