Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the limitation of 10^15 in D.J. Bernstein's 'primegen' program come from?

At http://cr.yp.to/primegen.html you can find sources of program that uses Atkin's sieve to generate primes. As the author says that it may take few months to answer an e-mail sent to him (I understand that, he sure is an occupied man!) I'm posting this question.

The page states that 'primegen can generate primes up to 1000000000000000'. I am trying to understand why it is so. There is of course a limitation up to 2^64 ~ 2 * 10^19 (size of long unsigned int) because this is how the numbers are represented. I know for sure that if there would be a huge prime gap (> 2^31) then printing of numbers would fail. However in this range I think there is no such prime gap.

Either the author overestimated the bound (and really it is around 10^19) or there is a place in the source code where the arithmetic operation can overflow or something like that.

The funny thing is that you actually MAY run it for numbers > 10^15:

./primes 10000000000000000 10000000000000100
10000000000000061
10000000000000069
10000000000000079
10000000000000099

and if you believe Wolfram Alpha, it is correct.

Some facts I had "reverse-engineered":

  1. numbers are sifted in batches of 1,920 * PRIMEGEN_WORDS = 3,932,160 numbers (see primegen_fill function in primegen_next.c)
  2. PRIMEGEN_WORDS controls how big a single sifting is - you can adjust it in primegen_impl.h to fit your CPU cache,
  3. the implementation of the sieve itself is in primegen.c file - I assume it is correct; what you get is a bitmask of primes in pg->buf (see primegen_fill function)
  4. The bitmask is analyzed and primes are stored in pg->p array.

I see no point where the overflow may happen.

like image 296
thinred Avatar asked Sep 27 '11 17:09

thinred


1 Answers

I wish I was on my computer to look, but I suspect you would have different success if you started at 1 as your lower bound.

like image 161
MartyTPS Avatar answered Sep 23 '22 15:09

MartyTPS