Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
For each dataset, prints the number of prime numbers.
Example
Input
10
3
11
Output
4
2
5
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to write a program that reads multiple integers until EOF (since number of datasets <= 30, no termination condition given except EOF). For each n (1 ≤ n ≤ 999,999), output the count of primes ≤ n.
The typical approach: precompute primes up to maximum possible n (999,999) using sieve of Eratosthenes, then answer queries by binary search or prefix sum array (count array). Since n up to 1e6 (actually 999,999), we can compute sieve of size 1,000,000 (maybe 1e6+1). Precompute prefix counts: primeCount[i] = number of primes ≤ i.
Then for each input n, output primeCount[n].
Edge case: n=1 -> primes less than or equal to 1: none (since 1 is not prime) => 0.
Implementation in C? The problem statement didn't specify language, but we can write in C or C++. Usually write in C.
We'll write:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000000
int main() {
bool is_prime[MAX+1];
int primeCount[MAX+1];
// initialize
for (int i=0; i<=MAX; i++) is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
for (int i=2; ii<=MAX; i++) {
if (is_prime[i]) {
for (int j=ii; j<=MAX; j+=i) {
is_prime[j] = false;
}
}
}
// compute prefix counts
primeCount[0] = 0;
for (int i=1; i<=MAX; i++) {
primeCount[i] = primeCount[i-1] + (is_prime[i] ? 1 : 0);
}
code
int n;
while (scanf("%d", &n) == 1) {
printf("%d\n", primeCount[n]);
}
return 0;
}
That's it.
We need to output for each dataset. Example: input 10,3,11 -> output 4,2,5.
Make sure to use efficient I/O? No.
Consider memory: bool array of size 1e6+1 fine.
Potential pitfall: Some judges might require that you read until EOF, and number of datasets unknown but ≤30.
We'll provide code accordingly.
If we need to handle n up to 999,999 inclusive, we can set MAX=1000000, but better set MAX=1000000 since we need up to 999,999. Use 1000000.
Alternatively, we could use 1000000 as limit, but since n could be 999,999, we need array index 999,999, so size 1000000 is enough (0..999,999). But to be safe, we might allocate 1000000+1 = 1000001. Let's use 1000000+1.
But note: If n=999,999, the sieve must mark composites up to that. So we need array size at least 1000000 (if we index from 0). Actually 999,999 is less than 1e6. We'll define MAX 1000000. So array indices from 0 to 1000000 inclusive? Wait: if MAX=1000000, we have indices 0..1000000 (size MAX+1). But we only need up to 999,999; but setting MAX=1000000 ensures we cover up to 1000000. But we can also define N=1000000. Let's do: #define N 1000000. Then we can use for loops up to N. But if n can be 999,999, primeCount[999999] is computed. That's fine. But in sieve, we need to mark multiples up to N, i.e., 1000000, which is okay even though not needed. So just use N=1000000.
However, caution: 1,000,000 is 10^6, which is not a prime obviously. So safe.
Alternatively, we could compute primes up to 1000000 using sieve.
Time complexity: O(N log log N) which is trivial.
We'll output each count on its own line.
Now the answer: we need to produce the program.
But the user asked: "Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n." So they expect a solution.
I'll provide final answer as code in C.
But note: the problem statement says "Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line. The number of datasets is less than or equal to 30." So multiple lines until EOF.
Thus final code.
I will also include comments.
Potential improvements: Use standard sieve.
Okay.
Now produce answer. ```c
#include <stdio.h>
#include <stdbool.h>
#define MAX 1000000 // maximum n (999,999) rounded up
int main() {
// ---------- sieve of Eratosthenes ----------
bool is_prime[MAX + 1];
for (int i = 0; i <= MAX; i++)
is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
code
for (int i = 2; i * i <= MAX; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= MAX; j += i)
is_prime[j] = false;
}
}
// ---------- prefix sums: primeCount[i] = number of primes ≤ i ----------
int primeCount[MAX + 1];
primeCount[0] = 0;
for (int i = 1; i <= MAX; i++)
primeCount[i] = primeCount[i - 1] + (is_prime[i] ? 1 : 0);
// ---------- answer queries ----------
int n;
while (scanf("%d", &n) == 1) {
printf("%d\n", primeCount[n]);
}
return 0;