数论一·Miller-Rabin质数测试

输入

第1行:1个正整数t,表示数字的个数,10≤t≤50

第2..t+1行:每行1个正整数,第i+1行表示正整数a[i],2≤a[i]≤10^18

输出

第1..t行:每行1个字符串,若a[i]为质数,第i行输出"Yes",否则输出"No"

样例输入
3
3
7
9
样例输出
Yes
Yes
No
解法提示:

有一种叫做Miller-Rabin质数测试的算法,可以很快的判定一个大数是否是质数。

这种质数算法是基于费马小定理的一个扩展,首先我们要知道什么是费马小定理:

费马小定理:对于质数p和任意整数a,有a^p ≡ a(mod p)(同余)。反之,若满足a^p ≡ a(mod p),p也有很大概率为质数。
将两边同时约去一个a,则有a^(p-1) ≡ 1(mod p)

也即是说:假设我们要测试n是否为质数。我们可以随机选取一个数a,然后计算a^(n-1) mod n,如果结果不为1,我们可以100%断定n不是质数。

否则我们再随机选取一个新的数a进行测试。如此反复多次,如果每次结果都是1,我们就假定n是质数。

该测试被称为Fermat测试。需要注意的是:Fermat测试不一定是准确的,有可能出现把合数误判为质数的情况。

Miller和Rabin在Fermat测试上,建立了Miller-Rabin质数测试算法。

与Fermat测试相比,增加了一个二次探测定理

如果p是奇素数,则 x^2 ≡ 1(mod p)的解为 x ≡ 1 或 x ≡ p - 1(mod p)

如果a^(n-1) ≡ 1 (mod n)成立,Miller-Rabin算法不是立即找另一个a进行测试,而是看n-1是不是偶数。如果n-1是偶数,另u=(n-1)/2,并检查是否满足二次探测定理即a^u ≡ 1 或 a^u ≡ n - 1(mod n)。

举个Matrix67 Blog上的例子,假设n=341,我们选取的a=2。则第一次测试时,2^340 mod 341=1。由于340是偶数,因此我们检查2^170,得到2^170 mod 341=1,满足二次探测定理。同时由于170还是偶数,因此我们进一步检查2^85 mod 341=32。此时不满足二次探测定理,因此可以判定341不为质数。

将这两条定理合起来,也就是最常见的Miller-Rabin测试。

但一次MR测试仍然有一定的错误率。为了使我们的结果尽可能的正确,我们需要进行多次MR测试,这样可以把错误率降低。

写成伪代码为:

Miller-Rabin(n):
	If (n <= 2) Then
		If (n == 2) Then
			Return True
		End If
		Return False
	End If
	
	If (n mod 2 == 0) Then
		// n为非2的偶数,直接返回合数
		Return False
	End If
	
	// 我们先找到的最小的a^u,再逐步扩大到a^(n-1)
	
	u = n - 1; // u表示指数
	while (u % 2 == 0) 
		u = u / 2
	End While // 提取因子2
	
	For i = 1 .. S // S为设定的测试次数
		a = rand_Number(2, n - 1) // 随机获取一个2~n-1的数a
		x = a^u % n
		While (u < n) 
			// 依次次检查每一个相邻的 a^u, a^2u, a^4u, ... a^(2^k*u)是否满足二次探测定理
			y = x^2 % n 
			If (y == 1 and x != 1 and x != n - 1)	// 二次探测定理
				// 若y = x^2 ≡ 1(mod n)
				// 但是 x != 1 且 x != n-1
				Return False
			End If
			x = y
			u = u * 2 
		End While
		If (x != 1) Then	// Fermat测试
			Return False
		End If
	End For
	Return True

值得一提的是,Miller-Rabin每次测试失误的概率是1/4;进行S次后,失误的概率是4^(-S)。

这个算法的时间复杂度,每一次单独的MR测试,需要O(log n)的时间。一共要进行S次MR测试,也就是O(Slog n)。

这样就能够在很短的时间内完成质数的测试了。当然如果你还是不放心,可以把S的值设定的更高一点。这样就能够顺利的找到大质数了。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>

using namespace std;

typedef long long LL;
//FILE *stream;
int t, S = 10;
LL num, u, a, x, y;

LL rand_Number(LL a, LL b)
{
	srand((unsigned)time(NULL));
	return rand() % (b - a + 1) + a;
}

LL mod_mul(LL a, LL b, LL n) {
	LL res = 0;
	while (b) {
		if (b & 1)    res = (res + a) % n;
		a = (a + a) % n;
		b >>= 1;
	}
	return res;
}

LL mod_exp(LL a, LL b, LL n) {
	LL res = 1;
	while (b) {
		if (b & 1)    res = mod_mul(res, a, n);
		a = mod_mul(a, a, n);
		b >>= 1;
	}
	return res;
}

bool Miller_Rabin(LL n)
{
	if (n < 2) return false;
	if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11)    return true;
	if (n == 1 || !(n % 2) || !(n % 3) || !(n % 5) || !(n % 7) || !(n % 11))    return false;
	
	// 我们先找到的最小的a^u,再逐步扩大到a^(n-1)
	u = n - 1;
	while (u % 2 == 0)
		u >>= 1;

	for (int i = 1; i <= S; ++i)
	{
		a = rand_Number(2, n - 1);
		x = mod_exp(a, u, n);//a^u % n  快速幂取模算法
		while (u < n)
		{
			// 依次次检查每一个相邻的 a^u, a^2u, a^4u, ... a^(2^k*u)是否满足二次探测定理
			y = mod_mul(x, x, n);//x * x % n
			if (y == 1 && x != 1 && x != n - 1)
				return false;
			x = y;
			u <<= 1;
		}
		if (x != 1)// Fermat测试
			return false;
	}
	return true;
}

int main()
{
	//freopen_s(&stream, "in.txt", "r", stdin);
	cin >> t;
	while (t--)
	{
		cin >> num;
		if ( Miller_Rabin( num ) )
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	//freopen_s(&stream, "CON", "r", stdin);
	//system("pause");
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值