Implementação do teste de primalidade de Fermat
Quem quer me ajudar com minha lição de casa?
Eu estou tentando implementarTeste de primalidade de Fermat em Java usando BigIntegers. Minha implementação é a seguinte, mas infelizmente não funciona. Alguma ideia?
public static boolean checkPrime(BigInteger n, int maxIterations)
{
if (n.equals(BigInteger.ONE))
return false;
BigInteger a;
Random rand = new Random();
for (int i = 0; i < maxIterations; i++)
{
a = new BigInteger(n.bitLength() - 1, rand);
a = a.modPow(n.subtract(BigInteger.ONE), n);
if (!a.equals(BigInteger.ONE))
return false;
}
return true;
}
Eu sou novo no BigIntegers.
Obrigado!