All problems

Problem 76

Count Primes

Medium · 20 pts · Math, Number Theory

Problem

You are given an integer N. Count the prime numbers that are strictly less than N. A prime is a whole number greater than 1 whose only divisors are 1 and itself. If N is 0, 1 or 2, there are no primes below it. Testing every number separately is too slow for N up to five million. Use the Sieve of Eratosthenes: mark all numbers as prime, then for each prime p starting from 2, cross out its multiples p × p, p × p + p, and so on. The numbers left unmarked are the primes.

Input

A single integer N.

Output

Print the number of primes below N.

Constraints

0 <= N <= 5000000

Example input

10

Example output

4

Explanation

The numbers below 10 are 0 to 9. Among them, 2, 3, 5 and 7 are prime, while 4, 6, 8 and 9 have other divisors. So there are 4 primes below 10.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Perfect Number Next: Prime Factorization