miller-rabin 测试不适用于 252097800623
miller-rabin test don't work for 252097800623
我正在尝试编写 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。当我在此代码中将所有整数更改为 long long 时,它们现在返回 NO。我还根据另一篇文章编写了自己的代码,当我用 11、101、17 甚至 1000000007 等小数字测试它时,它工作正常,但在 252097800623 等更大的数字上崩溃了。我想编写适用于所有整数的程序1 到 10^18
编辑
这里是修改后的代码形式 1st link:
/*
* 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;
}
您在问题中复制的第一个 link 中的代码,将(错误的)宏 ll
替换为 long long
(尽管这会产生完全相同的预处理代码)并且所有 int
和 long long
都已经因大值而损坏,请参阅 compiler explorer here。我强制编译器在编译时为 252097800623
计算 Miller
函数,用一个随机数 123456
.
替换对 rand()
的调用
如您所见,编译器告诉我它不能这样做,因为程序中存在整数溢出。特别是:
<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));
如您所见,long long
实在是太小了,无法处理该算法那么大的输入。
我正在尝试编写 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。当我在此代码中将所有整数更改为 long long 时,它们现在返回 NO。我还根据另一篇文章编写了自己的代码,当我用 11、101、17 甚至 1000000007 等小数字测试它时,它工作正常,但在 252097800623 等更大的数字上崩溃了。我想编写适用于所有整数的程序1 到 10^18
编辑
这里是修改后的代码形式 1st link:
/*
* 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;
}
您在问题中复制的第一个 link 中的代码,将(错误的)宏 ll
替换为 long long
(尽管这会产生完全相同的预处理代码)并且所有 int
和 long long
都已经因大值而损坏,请参阅 compiler explorer here。我强制编译器在编译时为 252097800623
计算 Miller
函数,用一个随机数 123456
.
rand()
的调用
如您所见,编译器告诉我它不能这样做,因为程序中存在整数溢出。特别是:
<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));
如您所见,long long
实在是太小了,无法处理该算法那么大的输入。