Fermatov test prvočíselnosti (cca)
Created: 2011-10-13 - 21:10
import java.math.BigInteger;
public class FermatPrimalityTest {
public static boolean testNumber(long n, int i) {
BigInteger a = new BigInteger(String.valueOf((long)(Math.random() * (n - 1))));
BigInteger nB = new BigInteger(String.valueOf(n));
if (i > 0) {
if (a.equals(a.modPow(nB,nB)))
return testNumber(n, i - 1);
else
return false;
} else
return true;
}
public static void main(String[] args) {
int maxPrime = 0;
for(int i = 1; i <= 100; i++) {
if(testNumber(i, 10)) {
if(i > maxPrime)
maxPrime = i;
}
}
System.out.println(maxPrime);
}
}