簡體   English   中英

BigIntegers對BigIntegers的影響

[英]BigIntegers to the power of BigIntegers

我正在嘗試使用BigInteger類在Java中實現Fermat,Miller-Rabin或AKS算法。

我想我已經實現了Fermat測試,只是BigInteger類不允許將BigIntegers發揮到BigIntegers的力量(一個人只能將BigIntegers發揮到原始int的力量)。 有沒有解決的辦法?

有問題的行在我的代碼中表示:

public static boolean fermatPrimalityTest(BigInteger n)
{
    BigInteger a;
    Random rand = new Random();
    int maxIterations = 100000;

    for (int i = 0; i < maxIterations; i++) {
        a = new BigInteger(2048, rand);

        // PROBLEM WITH a.pow(n) BECAUSE n IS NOT A BigInteger
        boolean test = ((a.pow(n)).minus(BigInteger.ONE)).equals((BigInteger.ONE).mod(n));

        if (!test)
            return false;
    }

    return true;
}

我認為BigInteger.modPow可能就是您想要的。 注意費馬測試中的“ mod m”。

原始性測試之一內置於BigInteger.isProbablePrime() 不知道是哪一個,您必須查看源代碼。

同樣,您可以通過乘數將數字提高到冪。 例如: 2^100 = 2^50 * 2^50 因此,請拉出BigInteger功能的一部分並循環,直到用完為止。 但你肯定你不是說要使用BigInteger.modPow()這需要BigInteger S' 根據您的測試,看起來像您。

您必須實現自己的pow()方法。 以BigInteger.pow()的來源為起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM