求教大家 谢谢了 初学不懂
#include "stdafx.h"
#include<stdio.h>
#include<math.h>
#include"iostream"
using namespace std;
bool btest(int a,int n);
bool millrob(int n);
bool repeatmillrob(int n,int k);
int _tmain(int argc, _TCHAR* argv[])
{
printf("2 3 ");
int n=5;
while(n<=10000)
{
int k = double(log10(double(n)));
if(repeatmillrob(n,k))
printf(" %d",n);
n=n+2;
}
return 0;
}
bool btest(int a,int n)
{
int s=0;
int t=n-1;
while(t%2 != 1)
{
s=s+1;
t=t/2;
}
int x=double(pow((double) a,(double) t));
x=x%n;
if(x==1||x==n-1)
return true;
int i=1;
while(i<s)
{
x=x*x%n;
if(x==n-1)
return true;
}
return false;
}
bool millrob(int n)
{
int goal=rand()%(n-1);
while(goal==0||goal==1)
{
goal=rand()%(n-1);
}
return btest(goal, n);
}
bool repeatmillrob(int n,int k)
{
int i=1;
while(i<=k)
{
if(millrob(n)==false)
return false;
}
return true;
}
程序将会打印 2 3 5 7 9 这个是素数测定的概率算法内容 当执行到n=11 k=1 为什么不调用repeatmillrob