1. Home
  2. Explore
  3. Mathematics
  4. Large Prime Number Generation

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Large Prime Number Generation

Speaker 1Speaker 2

Large Prime Number Generation

5:12
0:00-5:12
  1. Simon

    Have you ever wondered how computers generate prime numbers with thousands of digits? I mean, we know they exist, but actually finding one seems impossible if you try to check every divisor.

  2. Maria

    Right, the naive approach—dividing by every number up to the square root—just doesn't work for huge numbers. The universe isn't big enough to store all those calculations. Yet somehow, your browser uses these primes every time you visit a secure website.

  3. Simon

    Exactly. And it all goes back to a 17th-century mathematician: Pierre de Fermat. He discovered a property that every prime number has. It's called Fermat's Little Theorem. It says that if p is prime and a is any number less than p, then a to the power p minus 1, when divided by p, leaves a remainder of 1.

  4. Maria

    So that gives us a test? If we pick a base a and compute a^(n-1) mod n, if it's not 1, then n is definitely composite. That's the Fermat primality test, right?

  5. Simon

    Exactly. For example, test n=51 with a=2: 2^50 mod 51 is 4, not 1, so 51 is composite. And it works for primes like 17 with a=5: we get 1. But here's the catch—it can be wrong. There are composite numbers that satisfy the congruence for many bases. They're called Carmichael numbers.

  6. Maria

    Ah, like 561. 100^560 mod 561 equals 1, even though 561 is 3 times 11 times 17. So 100 is a liar. And for Carmichael numbers, most bases are liars—unless you happen to pick a base that shares a factor with n, which is basically as hard as factoring the number itself.

  7. Simon

    Right. And in 1994 it was proven that there are infinitely many Carmichael numbers, so the Fermat test has a fatal flaw. But in the early 1970s, a PhD student named Gary Miller came up with a better idea. He broke the exponentiation into a sequence of smaller steps.

  8. Maria

    So instead of just checking a^(n-1) mod n, you keep dividing the exponent by 2 until it's odd, and you check each intermediate result. That gives you a sequence of numbers. For n=561 with a=100, the sequence is 298, 166, 67, 1, 1.

  9. Simon

    And Miller's test has two conditions. First, the last number must be 1—that's just Fermat's condition. Second, whenever you see a 1 in the sequence, the number before it must be either 1 or n-1. In that sequence, before the final 1 we have 67, which is neither 1 nor 560. So 561 is correctly identified as composite.

  10. Maria

    That second condition comes from a neat property: if the modulus is prime, the only square roots of 1 are 1 and -1 (which is n-1). So finding a different square root of 1 proves compositeness. That's why Miller's test catches many numbers the Fermat test misses.

  11. Simon

    But Miller's test still isn't perfect. For example, n=221 with a=174 gives the sequence 47, 220, 1. Both conditions are satisfied, but 221 is 13 times 17. So 174 is a liar. However, Miller showed that for any odd composite, at least 50% of bases are witnesses.

  12. Maria

    And he also proved that if the Extended Riemann Hypothesis is true, then checking all bases up to about 2(ln n)^2 would give a deterministic test. But that hypothesis is still unproven, and relying on it for cryptography was too risky.

  13. Simon

    That's where Michael Rabin comes in. In 1980, he published a probabilistic version: just pick random bases. Since at least 75% of bases are witnesses, after 10 random bases, the chance of a false positive is less than 1 in a million. That's the Miller-Rabin test, and it became the gold standard.

  14. Maria

    And that's what's used today to generate large primes. The process is surprisingly simple: pick a random number with the desired number of digits, test it with Miller-Rabin. If it fails, pick another. Repeat until you find one that passes.

  15. Simon

    But you need that process to be efficient. That's where the Prime Number Theorem comes in. It says the chance a random n-digit number is prime is about 1 in 2.3n. For a thousand-digit number, that's roughly 1 in 2300. So after a couple thousand tries, you'll likely find one.

  16. Maria

    And computers can run a Miller-Rabin test in milliseconds, so generating a huge prime takes just a few minutes. For instance, you can generate a 2281-digit prime with 99.9999% confidence after just 10 random bases.

  17. Simon

    There is a completely deterministic test from 2002 called the AKS test, which is always 100% correct. But it's much slower, so in practice we still use Miller-Rabin. It's a beautiful example of how a little randomness and probability can solve a hard deterministic problem—and keep our data safe online.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent