All problems

Problem 350

Sum of Primes up to N

Medium · 20 pts · Math, Number Theory

Problem

Print the sum of all prime numbers less than or equal to N. Testing each number for primality separately is too slow for large N; the Sieve of Eratosthenes marks all composite numbers up to N in O(N log log N). The sum can exceed 32 bits, so use a 64-bit integer.

Input

A single integer N.

Output

Print the sum of the primes up to N.

Constraints

1 <= N <= 10000000

Example input

10

Example output

17

Explanation

The primes up to 10 are 2, 3, 5 and 7, and 2 + 3 + 5 + 7 = 17.

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

Previous: Largest Prime Factor Next: Sum of Consecutive Integers