素数知多少?
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1232 Accepted: 330
Description
在给定区间[1,n]中有多少个素数?
Input
每行一个整数n(1<=n<=1000000)
Output
[1,n]之间素数的个数,独立一行
Sample Input
1013
100
Sample Output
170
25
#include<stdio.h>
int arr[1000000] = { 0 };
int main()
{
int n;
while (scanf("%d", &n))
{
for (int i = 2; i <= n; i++)
{
if (arr[i]==0)
{
for (int j = i+i; j <= n; j+=i)
{
arr[j] = 1;
}
}
}
int cot = 0;
for (int i = 2; i <= n; i++)
{
if (arr[i] == 0)
{
cot++;
}
}
printf("%d\n", cot);
}
return 0;
}