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(): inputs = list(map(int, sys.stdin.read().split())) if not inputs: return max_n = max(inputs) sieve = [True] * (max_n + 1) sieve[0], sieve[1] = False, 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]) count = [0] * (max_n + 1) current = 0 for i in range(max_n + 1): if sieve[i]: current += 1 count[i] = current for n in inputs: print(count[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.