All problems

Problem 549

Twin Primes in a Range

Medium · 20 pts · Math, Sieve

Problem

Twin primes are two primes that differ by exactly 2, like 11 and 13. Count the pairs of twin primes (p, p + 2) that lie completely inside the range from L to R, meaning L <= p and p + 2 <= R. Sieve the primes up to R once, then check every p in the range. Note that (2, 4) is not a pair because 4 is not prime, and (3, 5) and (5, 7) are two different pairs.

Input

A single line with two integers L and R.

Output

Print the number of twin prime pairs inside [L, R].

Constraints

1 <= L <= R <= 1000000

Example input

1 20

Example output

4

Explanation

From 1 to 20 the twin prime pairs are (3, 5), (5, 7), (11, 13) and (17, 19). The pair (17, 19) fits because 19 <= 20, so the answer is 4.

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

Previous: Nth Prime Next: Goldbach Partitions