Miller-Rabin problem

Hi

Trying to code the miller-rabin primailty test, hit a problem and can't quite see where I am going wrong, err, help!

publicstaticboolean mr(BigInteger n,int t)throws Exception

{//Miller-Rabin Primality Test

srnd =new SecureRandom();

BigInteger s = n.subtract(ONE);

BigInteger r = ZERO;

BigInteger a = ZERO;

BigInteger y = ZERO;

while(s.mod(TWO).compareTo(ZERO)==0)

{

s = s.divide(TWO);

r = r.add(ONE);

}

for(int i = 1; i<=t; i++)

{

a =new BigInteger(n.subtract(TWO).bitCount(),srnd).add(TWO);

y = expm(a, r, n);

if(y.compareTo(ONE)!=0 && (y.compareTo(n.subtract(ONE))!=0))

{

BigInteger j = ONE;

while(j.compareTo(s.subtract(ONE))<=0 && y.compareTo(n.subtract(ONE))!=0)

{

y = expm(y, TWO, n);

if(y.compareTo(ONE)==0)

{

returnfalse;

}

j = j.add(ONE);

}

if(y.compareTo(n.subtract(ONE))!=0)

{

returnfalse;

}

}

}

returntrue;

}

Thanks

[2161 byte] By [cakea] at [2007-10-2 13:43:38]
# 1
OkClassic mistake, and only four hours of staring finally sorted it!!! The s and the t variables are the wrong way round!!!!Many thanks
cakea at 2007-7-13 11:39:15 > top of Java-index,Other Topics,Algorithms...
# 2
That's why everyone always tells you to give variables descriptive names...
DaanSa at 2007-7-13 11:39:15 > top of Java-index,Other Topics,Algorithms...