1

So I'm writing a primality test program, and somewhere along my code I have this:

BigInteger temp = BigInteger.valueOf(0);
BigInteger p_Big = BigInteger.valueOf(p); // p is just an integer

temp = power(a, p-1); // a method to calculate a^(p-1)
temp = temp.mod(p_Big);

if(temp != BigInteger.ONE){
    return false;
}

Problem is, I get false for values that should've returned true, and the weird thing is that when I do

System.out.println(temp+","+BigInteger.ONE);

for p = 5, a = 2 I get

1,1

So what's causing it to return false?

jimkokko5
  • 78
  • 1
  • 11

1 Answers1

1

Replace

if(temp != BigInteger.ONE)

with

if(!temp.equals(BigInteger.ONE))
Eran
  • 387,369
  • 54
  • 702
  • 768