I'm testing the property of Miller Rabin that the error probability is at most 1/4 when only a single base a is chosen and we iterate only one time. We are testing odd integers 90,000 to 100,000.
I've written up the implementation in Java and as the test is running, I'm seeing a lot of probabilities of .5. This leads me to believe that there is an issue with my implementation.
Some of the odd integers in which I'm seeing a .5 error probability are:
90007
91571
94343
There are plenty more (the test is still running).
Here is the implementation, if anyone could take a look and determine what the problem is I would really appreciate it.
Thanks
public BigInteger mr(int x, int y){
int u = 0;
BigInteger p = BigInteger.valueOf(x);
BigInteger r = p.subtract(ONE);
BigInteger a = BigInteger.valueOf(y);
while (r.mod(TWO).equals(ZERO)){
u++;
r = r.divide(TWO);
}
BigInteger z = a.modPow(r, p);
if ((!z.equals(ONE) && !z.equals(p.subtract(ONE)))){
int j = 1;
for (; j < u; j++){
z = z.modPow(TWO, p);
}
}
return z;
}
public boolean isPrime(int n){
if ( n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n) + 1; i+=2){
if (n % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
double ea;
MillerRabin mr = new MillerRabin();
int count = 0;
BigInteger ans;
for (int n = 90001; n< 100000; n+=2){
count = 0;
for (int a = 1; a < n; a++){
ans = mr.mr(n, a);
if (mr.isPrime(ans.intValue())){
count++;
}
}
ea = ((double)count) / (n-1);
System.out.println(ea);
}
}