Commits: 239 Resolved: 35
[ Commit ][ Status ][ Discussion Board ]
Topic description
Input an integer n, find the prime numbers within n, every ten lines, take the best possible algorithm.
enter
n
output
prime numbers up to n
sample input
100
Sample output
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
hint
source
Code:
#include<stdio.h>
int main()
{
int i,t;
int n,x=0;
int f(int a);
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(f(i))
x++,printf("%5d",i);
if(x%10==0)
printf("\n");
}
return 0;
}
int f(int a)
{
int i;
for(i=2;i<a;i++)
if(a%i==0)
return 0;
return 1;
}