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(): ns = [int(line.strip()) for line in sys.stdin if line.strip()] if not ns: return max_limit = 10**6 is_prime = [True] * (max_limit + 1) is_prime[0] = is_prime[1] = False for i in range(2, int(max_limit**0.5) + 1): if is_prime[i]: for j in range(i * i, max_limit + 1, i): is_prime[j] = False prime_count = [0] * (max_limit + 1) count = 0 for i in range(max_limit + 1): if is_prime[i]: count += 1 prime_count[i] = count for n in ns: print(prime_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.