Miller-rabin测试不适用于252097800623

问题描述 投票:-1回答:1

我正在尝试编写Miller-rabin测试。我发现了一些代码,例如:

https://www.sanfoundry.com/cpp-program-implement-miller-rabin-primality-test/https://www.geeksforgeeks.org/primality-test-set-3-miller-rabin/

当然,所有这些代码都适用于252097800623(这是质数),但这是因为它们正在将其解析为int。当我将此代码中的所有int值更改为long long时,它们现在返回NO。我还根据另一篇文章编写了自己的代码,当我用11、101、17甚至1000000007这样的小数字对其进行测试时,它可以工作,但是对于诸如252097800623之类的更大数字却显得有些破绽。我想编写适用于所有整数的程序1至10 ^ 18

编辑

这里是修改后的代码形式的第一个链接:

/* 

 * C++ Program to Implement Milong longer Rabin Primality Test

 */

#include <iostream>

#include <cstring>

#include <cstdlib>

using namespace std;



/* 

 * calculates (a * b) % c taking long longo account that a * b might overflow 

 */

long long mulmod(long long a, long long b, long long mod)

{

    long long x = 0,y = a % mod;

    while (b > 0)

    {

        if (b % 2 == 1)

        {    

            x = (x + y) % mod;

        }

        y = (y * 2) % mod;

        b /= 2;

    }

    return x % mod;

}

/* 

 * modular exponentiation

 */

long long modulo(long long base, long long exponent, long long mod)

{

    long long x = 1;

    long long y = base;

    while (exponent > 0)

    {

        if (exponent % 2 == 1)

            x = (x * y) % mod;

        y = (y * y) % mod;

        exponent = exponent / 2;

    }

    return x % mod;

}



/*

 * Milong longer-Rabin primality test, iteration signifies the accuracy

 */

bool Miller(long long p,long long iteration)

{

    if (p < 2)

    {

        return false;

    }

    if (p != 2 && p % 2==0)

    {

        return false;

    }

    long long s = p - 1;

    while (s % 2 == 0)

    {

        s /= 2;

    }

    for (long long i = 0; i < iteration; i++)

    {

        long long a = rand() % (p - 1) + 1, temp = s;

        long long mod = modulo(a, temp, p);

        while (temp != p - 1 && mod != 1 && mod != p - 1)

        {

            mod = mulmod(mod, mod, p);

            temp *= 2;

        }

        if (mod != p - 1 && temp % 2 == 0)

        {

            return false;

        }

    }

    return true;

}

//Main

int main()

{

    long long iteration = 5;

    long long num;

    cout<<"Enter long longeger to test primality: ";

    cin>>num;

    if (Miller(num, iteration))

        cout<<num<<" is prime"<<endl;

    else

        cout<<num<<" is not prime"<<endl;

    return 0;

}
c++ algorithm number-theory
1个回答
1
投票

您在问题中复制的第一个链接中的代码,用(C0)替换了(坏的)宏ll(尽管这会产生完全相同的预处理代码),而所有long long都是int,对于较大的值已经损坏,请参见long long。我强迫编译器在编译时评估compiler explorer hereMiller函数,将对252097800623的调用替换为一个随机数rand()

您可以看到编译器告诉我它不能这样做,因为程序中存在整数溢出。特别是:

123456

如您所见,<source>:133:17: error: static_assert expression is not an integral constant expression static_assert(Miller(num, iteration)); ^~~~~~~~~~~~~~~~~~~~~~ <source>:62:12: note: value 232307310937188460801 is outside the range of representable values of type 'long long' y = (y * y) % mod; ^ <source>:104:14: note: in call to 'modulo(123457, 63024450155, 252097800623)' ll mod = modulo(a, temp, p); ^ <source>:133:17: note: in call to 'Miller(252097800623, 5)' static_assert(Miller(num, iteration)); 太小,无法处理与该算法一样大的输入。

© www.soinside.com 2019 - 2024. All rights reserved.