#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
class judge_prime
{
public:
int Btest(int a,int n);
int MillRab(int n);
int RepeatMillRab(int n,int k);
};
int judge_prime:: Btest(int a,int n) //Miller-Rabin素性测试
{
int s=0;
int t=n-1;
int i=1;
int x=1;
int y;
do{ //n-1=m*2^k,求k的值
s++;
t=t/2;
}while((t%2)!=1);
while(i<=t)
{
x=(x*a)%n; //计算a^m mod n
i++;
}
if((x==1)||(x==n-1))return 1; //初始化到这里为止
for(int j=1;j<=s-1;j++)
{
x=(x*x)%n; //计算a^m^2^2^2...
if(x==n-1)return 1;
}
return 0;
}
int judge_prime::MillRab(int n)
{
int a;
srand((unsigned)time(0));
a=rand()%(n-3)+2;
return Btest(a,n);
}
int judge_prime::RepeatMillRab(int n,int k)
{
int i;
for(i=1;i<=k;i++)
{
if(MillRab(n)==0)return 0;
}
return 1;
}
int main()
{
int i;
int n=10000;
cout<<2<<" "<<3<<" ";
for(i=5;i<=n;)
{
judge_prime P;
if(P.RepeatMillRab(i,(int)log10((double)i)))
cout<<i<<" ";
i=i+2;
}
return 0;
}