The Las Vegas algorithm/method

A Las Vegas algorithm is a randomized algorithm that always gives the correct answer. Because of its nondeterministic nature, the run-time of a Las Vegas algorithm is a random variable.
An example of such an algorithm would be the randomized quickSort algorithm, it takes a random pivot point but at the end you get sorted data.
Scope: Las Vegas algorithms are used to solve some of NP Complete problems, Genetic algorithms, Evolution Strategies, Ant Colony Optimization and etc. The time to solve these is random, which means the duration shouldn’t be very big/long so another application of it is Cryptography application, generation of very long prime numbers and etc.
Miller-Rabin Primality test: An algorithm which determines whether a given number is prime, similar to the Fermat primality test and the Solovay-Strassen primality test.


#include <ctime>
using namespace std;
int RandInt(int Low, int High) //routine to return a random number between
{ //low and high boundaries
return rand( ) % ( High - Low + 1 ) + Low;
}
int Witness(int A, int i, int N) //if witness does not return 1 then N is
{ //definately composite. Fermat’s little
int X, Y; //theorem, says that if n is prime, then
//for all a between 1 and n - 1, an - 1 mod n = 1.
if( i == 0 ) //the contrapositive is if an - 1 mod n ? 1,
return 1; //then n is not prime. Take a randomly!
X = Witness( A, i / 2, N );
if( X == 0 ) // if N is recursively composite, stop
return 0;
Y = ( X * X ) % N; // N is not prime if we find a non-trivial root of 1
if( Y == 1 && X != 1 && X != N - 1 )
return 0;
if( i % 2 != 0 )
Y = ( A * Y ) % N;
return Y;
}
int CheckPrime( int N ) //repeat this procedure as many times as
{ //needed for desired error rate. Test if N >= 3 is
return Witness(RandInt(2, N-2), N-1, N) == 1; //prime using one value of A
}
int main()
{
srand(time(NULL)); //define random numbers
for(int i = 101; i<200; i+= 2) //check primality of a few numbers
if(CheckPrime(i)) //if number is prime print it
cout << i << ” is a prime number” << endl;
}



According to Wikipedia (http://en.wikipedia.org/wiki/Miller-Rabin) for smaller numbers, it’s only necessary to test “a” values of 2, 7 and 61.
I used these A values with the above code on the prime 15485863, and am told it is NOT a prime. Something is wrong somewhere.
S
@simes
A signed 32 bit integer is insufficient for that specific number.
Try the same algorithm with int64_t or an arbitrary precision integer library.