Write a program which reads an integer n and prints the number of prime numbers which…
- User
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
pythonimport sys def sieve(max_n): sieve = [True] * (max_n + 1) sieve[0] = sieve[1] = False for i in range(2, int(max_n ** 0.5) + 1): if sieve[i]: sieve[i*i : max_n+1 : i] = [False] * len(sieve[i*i : max_n+1 : i]) return sieve max_possible = 10**6 # Since n can be up to 999,999 sieve_list = sieve(max_possible) # Precompute the counts of primes up to each number counts = [0] * (max_possible + 1) count = 0 for i in range(max_possible + 1): if sieve_list[i]: count += 1 counts[i] = count # Read all input numbers and process for line in sys.stdin: n = int(line.strip()) print(counts[n])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.