My own IsPrime () function, written and based on a deterministic version of the well-known Rabin-Miller algorithm combined with optimized power-law forcing, which provides you with one of the fastest basic testing functions.
__int64 power(int a, int n, int mod) { __int64 power=a,result=1; while(n) { if(n&1) result=(result*power)%mod; power=(power*power)%mod; n>>=1; } return result; } bool witness(int a, int n) { int t,u,i; __int64 prev,curr; u=n/2; t=1; while(!(u&1)) { u/=2; ++t; } prev=power(a,u,n); for(i=1;i<=t;++i) { curr=(prev*prev)%n; if((curr==1)&&(prev!=1)&&(prev!=n-1)) return true; prev=curr; } if(curr!=1) return true; return false; } inline bool IsPrime( int number ) { if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) ) return (false); if(number<1373653) { for( int k = 1; 36*k*k-12*k < number;++k) if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) ) return (false); return true; } if(number < 9080191) { if(witness(31,number)) return false; if(witness(73,number)) return false; return true; } if(witness(2,number)) return false; if(witness(7,number)) return false; if(witness(61,number)) return false; return true; }
To use, copy and paste the code at the beginning of your program. Call it and it will return the BOOL value, true or false.
if(IsPrime(number)) { cout << "It prime"; } else { cout<<"It composite"; }
If you are having trouble compiling with "__int64", replace it with "long." It compiles for VS2008 and VS2010.
How it works: There are three parts to a function. The part checks whether this is one of the rare exceptions (negative numbers, 1) and intercepts the start of the program.
Part two begins if the number is less than 1373653, which is a theoretical number in which the Rabin Miller algorithm will surpass my optimized brute force function. Then there are two levels of Rabin Miller, designed to minimize the number of witnesses needed. Since most of the numbers you will be testing are less than 4 billion, the Rabin-Miller probabilistic algorithm can be determined by checking witnesses 2, 7 and 61. If you need to list a 4 billion cap, you will need a large one and apply a module or bit shift modification to the power () function.
If you insist on brute force, here is just my optimized IsPrime () function for brute force:
inline bool IsPrime( int number ) { if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) ) return (false); for( int k = 1; 36*k*k-12*k < number;++k) if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) ) return (false); return true; } }
How this brute force works: All prime numbers (except 2 and 3) can be expressed as 6k + 1 or 6k-1, where k is a positive integer. This code uses this fact and checks all numbers in the form 6k + 1 or 6k-1 less than the square root of the number in question. This part is integrated into my big IsPrime () function (the function shown first).
If you need to find all primes below the number, find all primes below 1000, look into the sieve of Eratosthenes. Another one of my favorites.
As an additional note, I would like someone to implement the Eliptical Curve Method algorithm, which wanted to see what has been implemented in C ++ for a while, I lost my implementation. Theoretically, this is even faster than the Rabin Miller deterministic algorithm that I implemented, although I'm not sure if this is true for numbers up to 4 billion.