#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int times=12;
ll mulit(ll a,ll b,ll mod){
ll ret=0;
while(b) {
if(b & 1) ret=(ret+a)%mod;
a=(a+a)%mod;
b >>= 1;
}
return ret;
}
ll quick_mod(ll a,ll b,ll mod) {
ll ret = 1;
while(b) {
if(b & 1) ret = mulit(ret,a,mod);
a = mulit(a,a,mod);
b >>= 1;
}
return ret;
}
bool check(ll a,ll n){
ll x = n - 1;
int t = 0;
while((x & 1) == 0) {
x >>= 1;
t ++;
}
x = quick_mod(a,x,n);
ll y;
for(int i=1;i<=t;i++) {
y = mulit(x,x,n);
if(y == 1 && x != 1 && x != n - 1) return true;
x = y;
}
if(y != 1) return true;
return false;
}
bool Miller_Rabin(ll n) {
if(n == 2) return true;
if(n == 1 || !(n & 1)) return false;
const int arr[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
for(int i = 0; i < times; i++) {
if (arr[i] >= n) break;
if(check(arr[i], n)) return false;
}
return true;
}
int main()
{
ll n;
while(cin>>n){
if(Miller_Rabin(n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
米勒罗宾素数测试
最新推荐文章于 2022-11-15 18:02:28 发布