我在玩素数和费马小定理。我读过关于Carmichael数字的文章,他们应该通过测试。问题是,当我测试它并使用两个不同的条件时,它应该以相同的结果结束,但事实并非如此。
代码:
import java.math.BigInteger;
public class FermatTest {
public static boolean passesAllFermatTests(BigInteger n) {
BigInteger testValue = BigInteger.ONE;
while (testValue.compareTo(n) == -1) {
if (!passesFermatTest(n, testValue)) {
return false;
}
testValue = testValue.add(BigInteger.ONE);
}
return true;
}
public static boolean passesFermatTest(BigInteger n, BigInteger a) {
//if( !a.modPow(n.subtract(BigInteger.ONE), n).equals(BigInteger.ONE)) {
if(! a.modPow(n, n).equals(a)) {
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(passesAllFermatTests(BigInteger.valueOf((long) 561)));
}
}当我在这个条件下运行它时,它返回true (通过它)。如果我使用注释条件运行它,它将返回false。应该是一样的,不是吗?是我的代码中有错误,还是我误解了什么?
https://stackoverflow.com/questions/41453475
复制相似问题