Keith Thompson said:
Phil Carmody said:
James Kuyper said:
[Absolutely none of the quoted matter]
I wouldn't recommend using floating point math (which is what IEEE 754
is about) to do a search for prime numbers.
Do you have any particular insights into the field that Yves Gallot,
Jean Penne, George Woltman, Ernst Mayer, Guillermo Ballester Valor,
Richard Crandall, Dan Bernstein and myself don't have?
Just append " unless you know what you're doing" and I'll have no
problems agreeing, but the people who write the code that helps
people find prime numbers (i.e. the above listed people) do know
what they're doing, and exclusively use floating point.
I find that very surprising; I would have thought that searching for
large prime numbers would call for the use of very large integers, not
floating-point (I assume we're talking about 64-bit floating-point).
To oversimplify tremendously, if you want to know whether
89587810616373014464315520632597 is prime, I don't see how being able
to represent 89587810616373014464315520632597.5 is helful. I presume
I'm missing something. (That's just a random number; it's probably
not prime.)
You certainly want the numbers that you start with, and end with,
in a multi-precision multiplication to be exact integers, split over
many limbs.
However, in order to keep the Big-Oh of multiplication down (and
prime hunters typically deal with large enough numbers that the
Big-Oh dominates constants) you want to be using a FFT-based
scheme. And as you probably know, that involves hitting floating
point numbers. (There are things called NTTs which don't, but
nobody's got them to actually be as quick as FP methods yet, due
to CPU designers putting all their effort into FPUs recently.)
Square(a) = InvFFT(AutoConvolution(FFT(a)))
Mult(a,b) = InvFFT(Convolution(FFT(a),FFT(b)))
Ignoring loglog terms, FFTs are O(n.log(n)), convolution's O(n).
So you've got an 'almost linear' O(n^(1+eps)) multiplication.
For comparison, high-school multiplication is O(n^2), and
Karatsuba and Toom Cook are O(n^(1+significant))
So it's FFTs for the win every time (if your numbers are big enough).
The problem is that every operation in the above adds what's
usually a small amount of random noise to the values. Log(n)
steps in the FFT, an extra square, and log(N) steps in the
InvFFT, and you can see that the error's not going to grow too
quickly on average. However, it only takes one outlier with an
error over 0.5, and you will round to the wrong value (these
can sometimes be detected). If you're accumulating several
thousand products into 1 limb, each original limb can't be
over about 20 bits, so it initially looks like the FFTs are
wasteful on space, but they're worth it in the long run.
Can you cite any references for this, particularly anything that
doesn't require higher math to understand?
V. shallow - just implies that FFTs are useful:
http://en.wikipedia.org/wiki/Multiplication_algorithm#Fourier_transform_methods
With comments about rounding errors, but in unreadable MS-specific HTML:
http://numbers.computation.free.fr/Constants/Algorithms/fft.html
(ISTR it's vaguely fixable with either John Walker's or my demoroniser,
http://fatphil.org/perl/)
These have everything, including dozens more references.
Crandall & Pomerance's /Prime Numbers, a Computational Perspective/
Dan Bernstein - /Multidigit Multiplication for Mathematicians/
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.20.8270
Phil