/***********************************************
名称:Miller_Rabbin素数测试程序
作者:ForrestGump
时间:2013-05-05
时间复杂度:O((logn)^3)
版本:v1.0
***********************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define s 100
double random()//生成[0~1]之间的均匀随机数
{
return (double)rand()/RAND_MAX;
}
int random(int m)//生成[1~m-1]之间的均匀随机数
{
return (int)(random()*(m-2)+1.5);
}
__int64 quick_mod(__int64 a,__int64 n,__int64 m)//快速求a的b次方mod c
{
__int64 d,t;
d=1;
t=a;
while (n>0)
{
if (n%2==1)//模拟二进制
d=(d*t)%m;
n/=2;
t=(t*t)%m;
}
return d;
}
bool Miller_Rabbin(__int64 n)//素数测试
{
for(int i=0;i<s;i++)
{
__int64 a=(__int64)random(n);
if(quick_mod(a,n-1,n)!=1)
{
return false;
}
}
return true;
}
int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF)
{
srand(time(NULL));//初始化随机数种子
if(Miller_Rabbin(n)&&n!=1)
printf("true\n");
else printf("false\n");
}
}
Miller_Rabbin 素数测试
最新推荐文章于 2020-09-08 21:19:16 发布