首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >费马素性检验与卡迈克尔数

费马素性检验与卡迈克尔数
EN

Stack Overflow用户
提问于 2017-01-04 06:38:33
回答 0查看 360关注 0票数 0

我在玩素数和费马小定理。我读过关于Carmichael数字的文章,他们应该通过测试。问题是,当我测试它并使用两个不同的条件时,它应该以相同的结果结束,但事实并非如此。

代码:

代码语言:javascript
复制
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。应该是一样的,不是吗?是我的代码中有错误,还是我误解了什么?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41453475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档