Slashdot Mirror


Fun with Prime Numbers

Steve Litt writes "Fun With Prime Numbers contains a series of prime number finding algorithms starting with the most brute force imaginable, and working up to a paged algorithm capable of finding the first 1,716,050,469 primes in an hour and a half on a commodity machine. There are faster algorithms on the net, but these algorithms are within the reach of mere mortals and are fully explained."

23 of 472 comments (clear)

  1. Obligatory by zedmelon · · Score: 5, Funny

    This article will be a prime target for bad jokes.

    --
    Mom says my .sig can beat up your .sig.
    1. Re:Obligatory by Anonymous Coward · · Score: 5, Funny

      We must not be divided on this issue.

  2. This begs the question: by Anonymous Coward · · Score: 4, Insightful

    Why not just save primes on a disk instead of recalculating them all the time?

    1. Re:This begs the question: by Anonymous Coward · · Score: 5, Funny

      Why waste disk space with primes instead of just recalculating them?

    2. Re:This begs the question: by Dasein · · Score: 4, Informative

      There are a lot of them.

      As a matter of fact 2048 bits can represent numbers up to 2^2048 ~= 3.23x10^616. By the Prime Number Theorem there is about 2.27x10^613 primes in that space. If each prime required 2048 bits to store, then storing all the 2048 bit primes requires more petabytes then you and I are likely to ever see.

      (Jeez I hope I didn't do the math wrong. The most convenient calc I have available to me as I write this in the Windows calc)

      --
      You are not a beautiful or unique snowflake -- but you could be if you got off your ass.
    3. Re:This begs the question: by Omniscient+Ferret · · Score: 4, Informative

      On my computer, generating primes from 2 to 2^32 takes a third of the time to unbzip2 the same list.

    4. Re:This begs the question: by maxwell+demon · · Score: 4, Interesting

      Indeed, the number of particles in the universe is assumed to be approximately 10^85, so if you would store one bit per particle, you'd need about 5*10^530 universes to store all those primes. Hell, even if you stored at every single particle as many bits as there are particles in the universe, you'd still need 5*10^445 universes.

      Of course that's assuming you stupidly write all those numbers individually down, using the whole 2048 bitf or each number. Now what if we compress the table?

      The complete list can, of course, be stored in a bitfield with 2^2048 individual bits which tell if a given number is prime or not (e.g. 1 means the number is prime, 0 means it's not). Now, of those 3.23*10^616 bits, about 2.27*10^613 bits are 1. So a simple approximation would be to treat the bits as independent, with each one having a probability of (2.27*10^613)/(3.23*10^616)=7.03*10^-4 of being 1. So if we neglect all correlations of the bits, we get an information of about 0.0084 bits per bitfield bit, which means we can compress to about 2.7*10^614 bits. That is, even neglecting all correlations (even the trivial ones of all even numbers except 2 being non-prime) we get only about 12 bits per prime. That's substantially less than the 2048 bits per prime we'd need with straightforward storage. Of course, we would still need an insane number of universes to store our table :-)

      Now, of course we know that we can "compress" the full table very well to a small prime-finding program which fits nicely on even a single floppy (but would take longer to "decompress" than we are willing to wait), so the list of primes doesn't actually contain too much information (indeed, the program can compress the whole infinite prime table - decompression of course needs unlimited ressources, then -, therefore the average information content of a single prime is obviously zero).

      --
      The Tao of math: The numbers you can count are not the real numbers.
    5. Re:This begs the question: by StyroCupMan · · Score: 5, Funny
      If each number only has a 0.000703 chance of being prime, we can simplify this whole calculation with this function (pseudo-coded):

      boolean isPrime(int Number) {
      return false;
      }
      That function is 92.97% accurate. That's an A-. Good enough for me. :)
      --
      If I may say so, life is a game, and there's so much to do and so few turns.
      -Reiner Knizia
  3. Re:Where is it? by canwaf · · Score: 5, Funny

    Sorry, 404 isn't a prime number. Try again.

  4. The author isn't a lightweight ... by xmas2003 · · Score: 4, Interesting
    While the prime number page is a bit odd (obligatory half-hearted pun), the author is Steve Litt who wrote Samba Unleashed ... plus there's already 40+ posts on the article, yet his web site is still pretty snappy despite the /. crowd ...

    BTW, the first bazillion prime numbers HAVE been calculated, so for those /.'ers with spare CPU cycles, consider something perhaps more worthwhile such as Folding@HOME

    --
    Hulk SMASH Celiac Disease
  5. Encryption by sbszine · · Score: 4, Interesting
    --

    Vino, gyno, and techno -Bruce Sterling

    1. Re:Encryption by sbszine · · Score: 5, Interesting

      Yeah, but that's an example of them being useful. It doesn't really make them esthetically "interesting".

      In that case I refer you to the fundamental theorum of arithmetic, which I think is pretty interesting.

      --

      Vino, gyno, and techno -Bruce Sterling

  6. Have some more fun with primes by pestilence4hr · · Score: 4, Informative

    A very fast segmented sieve (to find primes in an arbitrary range) can be found here, by Tomás Oliveira e Silva.

    Of tangential interest is his Goldbach conjecture verification project. You can download his client and contribute to the project. He's shooting for 10^18...

  7. the factor command in Unix/Linux by 3770 · · Score: 4, Interesting

    There is a command in many unices and Linux called factor.

    If you want to be a true geek you can try it on your friends phone numbers and find out if it is a prime.

    Then, the next time you talk, inform them that their phone number is a prime, or tell them of their phone numbers prime factorization, and enjoy watching them think that you are a super geek and a super genius.

    For even better effect, pretend to count in your head before you tell them this.

    --
    The Internet is full. Go Away!!!
    1. Re:the factor command in Unix/Linux by 0racle · · Score: 5, Funny

      You don't really have friends do you.

      --
      "I use a Mac because I'm just better than you are."
  8. Re:This begs the question by YGingras · · Score: 5, Informative
    What's the 1,716,050,470th prime?
    The 1,716,050,470th prime is 40,100,000,093.

    Find out more on the Nth Prime Page.
  9. Gaps between primes by tbjw · · Score: 5, Interesting

    From the article:

    who can be sure that there aren't two consecutive primes somewhere that are more than 65536 apart

    Let's recall elementary number theory:

    65537! + 2 = 2 * 3 * ... * 65537 + 2 = 2 * (3 * ... * 65537 + 1)
    65537! + 3 = 2 * 3 * ... * 65537 + 3 = 3 * (2* 4 * ... * 65537 + 1)
    ...

    65537! + 65537 = 2* 3 * ... * 65536 * 65537 + 65537 = 65537(65536!+1)

    So the 65536 consecutive numbers 65537! +2 , ..., 65537! + 65537 are all nonprime (the first is divisible by 2, the second by 3, the thrid by 4 etc). Since we also know that there are infinitely many primes, there must be a prime greater than 65537! + 65537. The biggest prime less than 65537! + 2 and the least prime bigger than 65537! + 65537 are consecutive primes which are at least 65536 apart.

    Of course, 65537! is a massive number, which is unlikely to crop up in these calculations. There may be other sufficiently large gaps between primes among smaller numbers.
    Consider, for instance, the method above applied with 5 in the place of 65536. We see that {6! +2, ..., 6! + 5} ={722, 723, 724, 725, 726} are all nonprime, but also 24, 25, 26, 27, 28 are 5 consecutive nonprime numbers.
    For where we can expect large 'prime-free' gaps, I'll defer to any number theorist.

  10. Re:What is special about prime numbers? by Jazzer_Techie · · Score: 5, Interesting

    I do not 'get' what is interesting or useful about prime numbers.

    Prime numbers are useful in encyption, but those are very large primes that are difficult to factor. To a mathematician, prime numbers are fascinating. The distribution of primes, which is specifially random but generally predictable (check out the MathWorld article for more details) is of particular interest.

    For example, the Riemann Zeta Function of n, which is the infinite sum of all terms k^(-n)[k varies from one to infinity], is expressable as the infinite product (1-p1^(-n))*(1-p2^(-n))*(1-p3^(-n))... [where pn is the nth prime number]. This is a mind-boggling connection. For example Zeta(2)= (pi^2)/6 = (1 + 1/2 + 1/4 +1/8 ...)= ((1-2^(-2))*(1-3^(-2))*(1-5^(-2))...) There is therefore a connection between pi and the distribution of the primes. Math is crazy stuff.

  11. Re:A better use? by benna · · Score: 4, Informative

    encryption.

    --
    "It is not how things are in the world that is mystical, but that it exists." -Ludwig Wittgenstein
  12. Not as much fun as by Snoe · · Score: 5, Funny

    The Prime Number Shitting Bear. Watching a console window spit out prime numbers might do it for the geeks, but everyone can loves the prime number shitting bear.

  13. I just RTFA... by acidblood · · Score: 5, Interesting

    ...and I'm going to list a few improvements here that I think the author missed.

    In one of the early programs, the author had the idea of skipping all even numbers, which are of course divisible by 2. In the next program he found out how he could skip numbers divisible by 3. One can make a systematic method out of that -- in the literature it's known as adding a wheel to the sieve of Erathostenes. Thing is, he implemented the sieve of Erathostenes foregoing this improvement he had implemented earlier. This leads to a big improvement: when using a wheel with the first four primes 2,3,5,7, there are only 48 residue classes out of 210 = 2*3*5*7 possible that are not divisible by any of these four primes. In practice this means that, for each 210 numbers you'd consider, the sieve would only need be applied in 48 of them. That's a gain of 4.375. One can make a gain of nearly 5 using a wheel of the first five primes, but it begins to get complicated.

    Another improvement would be to make the program cache-friendly. He sort of did this, down one level of the memory hierarchy, by paging the data to disk -- now he only needed to realize that, just as memory is much faster than disk, cache memory is faster than main memory. I've developed a sieve employing this optimization and it pays off indeed. This is considered a basic technique in the realm of high-performance computing. (The NFSNET, see my sig, uses a sieve including this and other optimizations, for instance).

    I might get flamed for this, but he should consider assembly programming. There aren't many applications where assembly will pay off so handsomely as this one, and the `core' of the sieve is very small, so only a few lines of assembly are needed. Vector integer instructions, such as MMX and the integer instructions of SSE2, can be employed to very good advantage here. My sieve program also included this, and it gives a real nice boost -- and only about 25 lines of assembly were used, the rest was C.

    Lastly there's the issue of representing the output. He stored the primes on disk by writing their values. A little compression can be realized in this representation: for instance he's printing out the primes in decimal, while hexadecimal would be more space-efficient. And instead of printing out the ASCII characters from 0 to 9, A to F, he could use a binary representation and read them through an hex dump -- that saves half the space, as a byte is used to store two hexadecimal digits instead of one.

    A perfectly good representation that could save a lot of space (8 times, to be precise) would be a bitmap, which he used on some of his programs. That's where you represent primality or lack of it by setting and clearing bits of an integer. It's pretty easy to determine a prime from a bitmap value, so this representation is just as good as the previous one. One could also use the wheel idea I explained before to save even more space: a concrete example would be that for each block of 210 numbers, one needs only bitmaps for 48 of them, as the rest are divisible by the first four primes. In this example a savings of 4.375 would be realized. Of course, all these ideas would require special readers programs, instead of just opening up the files in a text editor -- a small price to pay for such a large space savings.

    But in case only sequential reading of the primes is desired, there is an even more efficient representation: just write out the prime gaps, i.e. the difference between consecutive primes. One bit can be saved by realizing that, other than 3 - 2, all prime gaps are even, so one can just store the prime gap divided by two. One could really nitpick and point out that 0 is an impossible prime gap, so one could subtract two from all gaps, but this gain is too small to be worth the hassle. According to Thomas Nicely (incidentally the guy who discovered the bug on the Pentium FPU during his studies on computational number theory), one could store the prime gaps of all

    --

    Join the NFSNET. Our prime goal is making little numbers out of big ones. http://www.nfsnet.org/

  14. Re:Mersenne by acidblood · · Score: 4, Informative

    I'd venture to say I know what I'm talking about. One can't publish a paper about large-scale distributed primality proofs without knowing these basics. Here's a sample of my work. Now don't get me wrong, I'm not trying to sound like a smart-ass, but I'm not going to let someone question my knowledge in this field (which I have studied for years) and remain quiet.

    Now it seems people here are not familiar with the terminology associated with Mersennes, so I'll give a crash course.

    A Mersenne number M_n is of the form 2^n - 1. If 2^n - 1 is prime, then M_n is a Mersenne prime. If 2^n - 1 is composite, then M_n is a Mersenne composite. The integer n in this expression is called the exponent. When I talk about `exponents of Mersenne composites', I'm refering to some prime n for which M_n = 2^n - 1 is composite.

    Now if 2^n - 1 is prime, then n must be prime, but the converse is not always true, the smallest counterexample being 2^11 - 1 = 2047 = 23*89. Turns out that for prime exponents n = 24,036,583, only in 41 cases is 2^n - 1 actually prime. A list of these cases can be found here. For the other 1,509,222 prime values of n = 24,036,583, 2^n - 1 is composite. This translates to a compositeness ratio of 99.9973%.

    I hope everything is clearer now.

    --

    Join the NFSNET. Our prime goal is making little numbers out of big ones. http://www.nfsnet.org/

  15. How to prove that all odd numbers are prime by dargaud · · Score: 5, Funny

    "It was mentioned on CNN that the new prime number discovered recently is four times bigger than the previous record." --John Blasik

    "You know what seems odd to me? Numbers that aren't divisible by two." --Michael Wolf.

    "I don't get even, I get odder."

    How to prove that all odd numbers are prime? Well, this problem has different solutions whether you are a:

    • Mathematician: 1 is prime, 3 is prime, 5 is prime, 7 is prime, and by induction we have that all the odd integers are prime.
    • Physicist: 1 is prime, 3 is prime, 5 is prime, 7 is prime, 9 is an experimental error...
    • Engineer: 1 is prime, 3 is prime, 5 is prime, 7 is prime, 9 is prime...
    • Chemist: 1 prime, 3 prime, 5 prime... hey, let's publish!
    • Modern physicist using renormalization: 1 is prime, 3 is prime, 5 is prime, 7 is prime, 9 is ... 9/3 is prime, 11 is prime, 13 is prime, 15 is ... 15/3 is prime, 17 is prime, 19 is prime, 21 is ... 21/3 is prime...
    • Quantum Physicist: All numbers are equally prime and non-prime until observed.
    • Professor: 1 is prime, 3 is prime, 5 is prime, 7 is prime, and the rest are left as an exercise for the student.
    • Confused Undergraduate: Let p be any prime number larger than 2. Then p is not divisible by 2, so p is odd. QED
    • Measure nontheorist: There are exactly as many odd numbers as primes (Euclid, Cantor), and exactly one even prime (namely 2), so there must be exactly one odd nonprime (namely 1).
    • Cosmologist: 1 is prime, yes it is true....
    • Computer Scientist: 1 is prime, 10 is prime, 11 is prime, 101 is prime...
    • Programmer: 1 is prime, 3 is prime, 5 is prime, 7 is prime, 9 will be fixed in the next release, ...
    • C programmer: 01 is prime, 03 is prime, 05 is prime, 07 is prime, 09 is really 011 which everyone knows is prime, ...
    • BASIC programmer: What's a prime?
    • COBOL programmer: What's an odd number?
    • Windows programmer: 1 is prime. Wait...
    • Mac programmer: Now why would anyone want to know about that? That's not user friendly. You don't worry about it, we'll take care of it for you.
    • Bill Gates: 1. No one will ever need any more than 1.
    • ZX-81 Computer Programmer: 1 is prime, 3 is prime, Out of Memory.
    • Pentium owner: 1 is prime, 3 is prime, 5 is prime, 7 is prime, 8.9999978 is prime...
    • GNU programmer: % prime
      usage: prime [-nV] [--quiet] [--silent] [--version] [-e script] --catenate --concatenate | c --create | d --diff --compare | r --append | t --list | u --update | x -extract --get [ --atime-preserve ] [ -b, --block-size N ] [ -B, --read-full-blocks ] [ -C, --directory DIR ] [--checkpoint ] [ -f, --file [HOSTNAME:]F ] [ --force-local ] [ -F, --info-script F --new-volume-script F ] [-G, --incremental ] [ -g, --listed-incremental F ] [ -h, --dereference ] [ -i, --ignore-zeros ] [ --ignore-failed-read ] [ -k, --keep-old-files ] [ -K, --starting-file F ] [ -l, --one-file-system ] [ -L, --tape-length N ] [ -m, --modification-time ] [ -M, --multi-volume ] [ -N, --after-date DATE, --newer DATE ] [ -o, --old-archive, --portability ] [ -O, --to-stdout ] [ -p, --same-permissions, --preserve-permissions ] [ -P, --absolute-paths ] [ --preserve ] [ -R, --record-number ] [ [-f script-file] [--expression=script] [--file=script-file] [file...]
      prime: you must specify exactly one of the r, c, t, x, or d options
      For more information, type "prime --help''
    • Unix programmer: 1 is prime, 3 is prime, 5 is prime, 7 is prime, ...
      Segmentation fault, Core dumped.
    • Computer programmer: 1 is prime, 3 is prime, 5 is prime
    --
    Non-Linux Penguins ?