#include "stdio.h"
int prime(int n){//素数判断
int i;
if(n>2 && !(n&1) || n<2)
return 0;
for(i=3;i*i<=n;i+=2)
if(!(n%i))
return 0;
return 1;
}
int main(int argc,char *argv[]){
int n,s,i;
printf("Please enter n(int 0if(scanf("%d",&n)!=1 || n<1){
printf("Input error, exit...\n");
return 0;
}
for(s=0,i=2;i<=n;i++)
if(prime(i)){
printf("%d ",i);
s++;
}
printf("\nA total of %d prime.\n",s);
return 0;
}
运行样例:
#include
int isprime(int n)
{ int i;
for(i=2; i*i<=n; i++)
if(n%i==0)return 0;
return n>1;
}
int main()
{ int n,i,s=0,k=0;
scanf("%d",&n);
for(i=2; i<=n; i++)
if(isprime(i))
{ s+=i;
k++;
}
printf("共%d个素数,总和为%d\n",k,s);
return 0;
}
#include
void main() { int i,j,n,s,m,b;
scanf("%d",&n); m=s=0;
for ( i=1;i<=n;i++ ) {
b=1; for ( j=1;j<=i/2;j++ ) if ( i%j==0 ) { b=0;break; }
if ( b!=0 ) { printf("%d ",i); s+=i; m++; }
}
printf("\n1--%d之间有素数%d个,素数和为%d\n",n,m,s);
}