Yes, you will find with most algorithms that you can exchange space for time. In other words, allowing you to use more memory, the speed increases significantly * a .
I really do not know the Miller-Rabin algorithm, but if it is not easier than a single extraction of the left-shift / extra shift and memory, it will be removed from the water by a pre-calculated sieve.
The important thing here is pre-calculated. It's a good idea, in terms of performance, to pre-compute such things, since the first millions of primes are unlikely to change in the near future :-)
In other words, create your own sieve with something like:
unsigned char primeTbl[] = {0,0,1,1,0,1,0,1,0,0,0,1};
#define isPrime(x) ((x < sizeof(primeTbl) ? primeTbl[x] : isPrimeFn(x))
with all the usual warnings not to pass things like a++macros. This gives you the best of both worlds, a dazzlingly fast search for tables for "small" primes, a return to the calculation method for those who are out of range.
, , - .
, , , !
* a - , . , , CPU grunt.
, .
? , . ( 90 ) ( , ), 180 ( ).
- : -)
, C ββ, (283 000 ).
#include <stdio.h>
static unsigned char primeTbl[4000000];
int main (void) {
int i, j;
for (i = 0; i < sizeof(primeTbl); i++)
primeTbl[i] = 1;
primeTbl[0] = 0;
primeTbl[1] = 0;
for (i = 2; i < sizeof(primeTbl); i++)
if (primeTbl[i])
for (j = i + i; j < sizeof(primeTbl); j += i)
primeTbl[j] = 0;
printf ("static unsigned char primeTbl[] = {");
for (i = 0; i < sizeof(primeTbl); i++) {
if ((i % 50) == 0) {
printf ("\n ");
}
printf ("%d,", primeTbl[i]);
}
printf ("\n};\n");
printf ("#define isPrime(x) "
"((x < sizeof(primeTbl) ? primeTbl[x] : isPrimeFn(x))\n");
return 0;
}
primeTbl (16M), , , ( 1031, 130 ).
, , , , . , .