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 main(): max_n = 999999 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]) counts = [0] * (max_n + 1) count = 0 for i in range(max_n + 1): if sieve[i]: count += 1 counts[i] = count for line in sys.stdin: n = int(line.strip()) print(counts[n]) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.