课上测试6.9

 密码工程-小素数
任务详情
0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
1. 参考《密码工程》p107伪代码基于Eratosthenes算法实现 int SmallPrimeList(int n, int *plist, int *len), 其中plist返回素数列表,len返回列表长度(10
’)
2 写出测试代码,至少包括 n=2, n=你的四位学号,n>2^20次方的测试代码,提交代码和运行结果截图(5)

代码:
#include <stdio.h>

int main() {
int i,n,x,j,k,a;
scanf("%d",&n);
n=n+1;
while(n>=1048576){
printf("%s","n is too large\n");
scanf("%d",&n);
}
int b[n];
int P[n];
for(x=0;x<n;x++)
{
b[x]=1;
}
i=2;
while(i*i<=n){
for(x=2;x<=(n/i);x++)
{
b[x*i]=0;
}
i=i+1;
}

a=0;
printf("%s","the list:");
for(k=2;k<=n;k++){
 if(b[k]==1){
  printf("%d ",k);
  a=a+1;
 }
}
printf("%s","\n the lenth :");
printf("%d",a);
return 0;
}
posted @ 2022-06-09 14:46  20191229XYZ  阅读(30)  评论(0)    收藏  举报