# 函数问题之寻找素数

//提交时只需提交getprime函数
#include<stdio.h>
#include<string.h>
int prime[10000];
/*

*/
int main()
{
int getprime(int left,int right);
int left,right;
scanf("%d%d",&left,&right);
int i,n=getprime(left,right);
printf("%d\n",n);
for(i=1;i<n;i++)

printf("%d ",prime[i]);

if(i == n)

printf("%d\n",prime[i]);
return 0;
}

``2 5``

``````3
2 3 5``````

``````#include<stdio.h>
#include<string.h>
int prime[10000];

int getprime(int left,int right)
{
if(right<2)
return 0;
if(left<=2)
left=2;
int i,j,o=0;
for(i=left;i<=right;i++){
for(j=2;j*j<=i;j++)
if(i%j==0)
break;
if(j*j>i){
o++;
prime[o]=i;
}
}
return o;
}
int main()
{
int getprime(int left,int right);
int left,right;
scanf("%d%d",&left,&right);
int i,n=getprime(left,right);
printf("%d\n",n);
for(i=1;i<n;i++)
printf("%d ",prime[i]);
if(i == n)
printf("%d\n",prime[i]);
return 0;
}
``````

``````#include<stdio.h>
#include<string.h>
int prime[10000];

int getprime(int left,int right)
{
int a[right+1],i,j,b=1;
for(i=2;i<=right;i++)
a[i]=0;
for(i=2;i<=right;i++)
if(a[i]==0)
{
if(i<=right&&i>=left)
{prime[b]=i;
b++;}
for(j=i+i;j<=right;j=j+i)
a[j]=1; }
return b-1;
}
int main()
{
int getprime(int left,int right);
int left,right;
scanf("%d%d",&left,&right);
int i,n=getprime(left,right);
printf("%d\n",n);
for(i=1;i<n;i++)
printf("%d ",prime[i]);
if(i == n)
printf("%d\n",prime[i]);
return 0;
}
``````