The Lucas-Lehmer Prime Number Test

(scientificamerican.com)

96 points | by beardyw 5 months ago ago

57 comments

  • jmount 4 months ago

    The original Agrawal, Kayal, Saxena "Primes is in P" paper is actually an amazing effort in clarity https://annals.math.princeton.edu/wp-content/uploads/annals-... .

  • oliviersca 4 months ago

    For those who enjoy burning cpu cycles ! m1277 = 2 ^ 1277 - 1 is not prime. It easy to check it with the Lucas-Lehmer test. But we don't know any of its divisors, which is quite fascinating.

  • roflchoppa 4 months ago

    The Lehmers are cool people… Ron’s got a train spotting site.

    http://calcoastrails.com/

    • jrockway 4 months ago

      I knew about this site and knew about the Lucas-Lehmer test, but I never would have made the connection between the two. People are kind of amazing.

      • roflchoppa 4 months ago

        I knew Derrick Lehmer Jr before he passed away, just happened to know the name.

  • 30030 4 months ago

    That's easy.

    (factorial(170,141,183,460,469,231,731,687,303,715,884,105,726) + 1)%(170,141,183,460,469,231,731,687,303,715,884,105,727) == 0

  • IsTom 4 months ago

    My favourite prime checking algorithm is that for n < 100 if it looks prime, it is prime.

    • freehorse 4 months ago

      Up to 100 it is relatively simple, if you remove the even numbers, the multiples of 3 (summing up their digits recursively gives 3 6 or 9), and those that end at 5, you end up with 25 numbers out of which 22 (88%) are primes. If you further exclude 49 which you should remember is 7^2 from the multiplication table, and 77 that is obviously divisible by 7, you are left with 22/23 chance a number that passes these exclusion criteria is not 91 and hence it is a prime.

      • maxbond 4 months ago

        > (summing up their digits recursively gives 3 6 or 9)

        My fun fact is that this type of operation (repeatedly applying a child operation until you reach a fixed point) is called persistence.

        https://en.wikipedia.org/wiki/Persistence_of_a_number

        The fixed point here being that if you add up a list of 1 digits, you'll always reach the same number (`sum([1]) = 1`). The best known is probably the hailstone sequence.

        https://en.wikipedia.org/wiki/Collatz_conjecture

        I'm partial to multiplicative persistence.

        https://www.youtube.com/watch?v=Wim9WJeDTHQ [15m]

      • jeffbee 4 months ago

        > summing up their digits recursively gives 3 6 or 9

        What does this part mean? For example 57.

        • zdimension 4 months ago

          57 is 3 times 19.

          The standard divisibility rule for 3, 6 and 9 in base 10 is to sum the digits until you only have one left and check if it's one of those. Here, 5+7=12, 1+2=3, so 57 is divisible by 3.

          • squigz 4 months ago

            Math is not my strong suit at all, so I probably won't grok this, but that kind of blows my mind, so I'm curious... how?! That works for any arbitrarily large number?

            Math is crazy!... still don't want to study it though!

            • moring 4 months ago

              Yes. A number like

              123456 = 1 * 100000 + 2 * 10000 + 3 * 1000 + 4 * 100 + 5 * 10 + 6 = 1 * (99999+1) + 2 * (9999+1) + 3 * (999+1) + 4 * (99+1) + 5 * (9+1) + 6

              When checking whether it is a multiple of some k, you can add/subtract multiples of k without changing the result, and those 99...9 are multiples of both 3 and 9.

              So 123456 is a multiple of 3 (or 9) iff

              1 * 1 + 2 * 1 + 3 * 1 + 4 * 1 + 5 * 1 + 6 * 1 = 1 + 2 + 3 + 4 + 5 + 6

              is. Apply the same rule as often as you want -- that is, until you only have one digit left, because then it won't get simpler anymore.

            • freehorse 4 months ago

              It is basically because $10 mod 3 == 1$ (as 10 = 3*3 + 1). So if you are in the ring modulo 3, where every number is equal to the remainder of its division by 3, the sum of the digits of the number in its decimal representation equals the number itself (modulo 3), because in that ring 10 is actually 1, so the 10s in the decimal sum become 1s. Ie if n_k is the kth digit of n, you have

                  (mod 3) n == n_0 + n_1*10 + n_2*10^2 + ... == n_0 + n_1 + n_2 + ...
              
              Hence, n is divisible by 3 iff $n mod 3 == 0$ iff $(n_0 + n_1 + n_2 + ...) mod 3 == 0$.

              Of course, summing up the digits may not give you a 1-digit number, but it gives you a number that you know is divisible by 3 (if the original number is divisible by 3). So you can apply the same idea/process again, summing up the digits of that number, and get another number that is divisible by 3. Repeat until you end up with one digit (hence the recursion mentioned).

            • 4 months ago
              [deleted]
          • jeffbee 4 months ago

            Ah I see what is meant by recursively here. Thanks!

        • hollerith 4 months ago

          5 plus 7 is 12, which of course has a 1 and a 2, the sum of which is 3.

    • throwaway81523 4 months ago

      Like the famous Grothendieck prime of course.

      • xorbax 4 months ago

        Definitely makes me feel better about my own work

    • floydian10 4 months ago

      91 should be prime, ridiculous that it isn't

      • sunrunner 4 months ago

        Agree. Plus now I now need to release a security patch for the hand-rolled crypto library I built.

    • emaccumber 4 months ago

      The annoying child in me will always remember correcting my freshman math teacher when he needed a prime number and wrote 91 on the chalkboard.

    • sunrunner 4 months ago

      I wonder what the underlying human intuition is for 'prime-ness' and why it might break down with larger numbers. Odd numbers in the rightmost position? The 'shape' of the number (phonaesthesia, the bouba/kiki effect)? Maybe they just sort of feel scary?

      • IsTom 4 months ago

        For n < 100 to be composite you need a factor < sqrt(100) = 10. Rules for 2, 3, 5 are easy to try quickly. That leaves 7, but up to 7*9 you should remember it from multiplication tables. 77 is quite obviously divisible by 11 too, and then it's 7*13 = 91 as the last boss. But I feel that once you realize how special 91 is in that context, you won't forget it again.

    • conradev 4 months ago

      This only works if you know multiplication tables which is not a given in America these days.

      • gosub100 4 months ago

        "not given" != "not able to learn"

    • GMoromisato 4 months ago

      Are there any numbers that don't look prime but are, in fact, prime? [Other than 2, I suppose.]

      • andruby 4 months ago

        11, 13, 17 and 19 used to trip me up. And maybe 67

        • mootothemax 4 months ago

          67 has absolutely no right to be prime. Sitting there looking all innocent.

          • bombcar 4 months ago

            Maybe that’s the real secret behind the 6-7 meme going around

    • ridiculous_fish 4 months ago

      Except 91.

    • 4 months ago
      [deleted]
    • 4 months ago
      [deleted]
    • chrisshroba 4 months ago

      Except 91

    • wbl 4 months ago

      57

  • John-Tony 4 months ago

    [dead]

  • Nevermark 4 months ago

    Just grab some paper, a pen, and check that no number equal or smaller than its square root divides into it evenly.

    That is it. That is all. Pish posh.

    • WCSTombs 4 months ago

      The example given in the article is 2^127 - 1, which was historically proved to be prime without computers using a clever method now known as the Lucas-Lehmer test. Your algorithm is not practical for that number.

      • Nevermark 4 months ago

        Ah, but I can assure you, it is just that simple.

        If a number is not prime, then it is the product of at least two numbers smaller than itself.

        If any of them are larger than its square root, all others must be smaller, or their product would be larger than the candidate prime.

        Ergo, just check that the candidate is not evenly divisible by any number equal or lower than its square root.

        This reasoning holds, independent of scale.

        QED. Check mate. Shazam.

        • xpe 4 months ago

          The obvious and naive method described above is O(sqrt(N)). For N ~= 2 ^ 127, that is about 2 ^ 64. / The Lucas-Lehmer method described in the article is better (how much better is an exercise for the reader).

          • gsliepen 4 months ago

            You are assuming division itself is an O(1) operation. However, it also scales with the size of the number. So more correct would be to say that this naive method is O(sqrt(N) log(N) log(log(N))).

            • xpe 4 months ago

              I was both hoping and fearing that someone would correct my simplification. (That is more nested logs than I would’ve guessed however.)

        • mysterydip 4 months ago

          Perfect example of how "if the code is compact, it's fast" can be deceiving :)

          • Nevermark 4 months ago

            Search is all you need!

            If you have the time.

            Or if we can expand quantum superposition algorithms from 2^N states, for quantum circuits with N control qubits, to 2^(T*N) superpositions over T time steps, via some kind of superposition tree recursion. The number of superpositions increasing exponentially for T steps (and then reducing for another T steps) on a single recursive physical circuit.

            That is not supported by the physical laws we have, but it is an interesting idea.

        • 4 months ago
          [deleted]
      • great_wubwub 4 months ago

        /r/whoosh

        • eps 4 months ago

          Nah, the joke just was fairly flat and low-effort.

    • tmtvl 4 months ago

      Even better: you only have to check primes smaller than or equal to the square root.

  • NetMageSCW 4 months ago

    Pay wall.

    • WolfeReader 4 months ago

      Imagine browsing the web without an ad blocker.

  • politelemon 4 months ago

    [flagged]

    • zamadatix 4 months ago

      The scourge of HN submission rules - "you can submit anything and it's up to everyone else to actually be able to access it".

      https://archive.is/8R0Fq

      • tomhow 4 months ago

        The HN rule is that anything can be posted if it is accessible to everyone, including via a paywall workaround if it has a paywall that is porous.

        The community has converged on this being the least-worst approach after wrestling with the issue for well over a decade, and it's sufficient to helpfully post the archive link without a snarky editorial comment :)

      • Terr_ 4 months ago

        If I had my 'druthers designing a new link-share/comment system, the visibility (and mirrors or excerpts) for the target would be part of the model.

        In other words, an icon showing whatever-wall status, submitter can add an alternate link, etc.

    • smcin 4 months ago

      The standard etiquette on HN is not to bother saying that, just post its archive link already.

    • xeonmc 4 months ago

      I guess that's the answer then -- you need a subscription instead of a computer.

    • NSPG911 4 months ago

      i found out that most articles behind paywalls dont even need the wayback machine to be viewed

      if you are using firefox, just enter reading mode and you can read the entire article without popups in your preferred background, text color, font, etc