This page is still under construction.

Parts of this page are still being built. What you see may change.

Truncatable Primes

Time limit1sMemory limit512 MB

Summary
Count integers in [a, b] whose every prefix from the left is prime.
Level

Medium7 of 10

Topics
Backtracking, Number theory
Solved
No attempts yet

Problem

Recall that a prime is a positive integer with exactly two distinct divisors: 11 and itself. We say that a number aa is a prefix of a number bb if aa can be obtained by deleting some number of digits from the end of bb. For example, 12311231 is a prefix of 1231443312314433. A truncatable prime is a number all of whose prefixes of non-zero length are prime. For example, 2323 is a truncatable prime, because its non-empty prefixes 22 and 2323 are both prime.

Given two positive integers aa, bb (a≤ba \le b), write a program that determines how many integers in the closed interval [a,b][a, b] are truncatable primes.

Input

A single line of standard input contains two integers aa, bb (1≤a≤b≤10181 \le a \le b \le 10^{18}), separated by a space.

Output

Print a single integer: the number of truncatable primes that are not less than aa and not greater than bb.

Hint

There are only finitely many such numbers. The first digit must be one of the single-digit primes 22, 33, 55, 77, and every time you append a digit on the right the resulting number must still be prime. So you can generate all such numbers in advance and then count those that fall inside the interval.

Examples3

  1. Example 1

    Input
    20 24
    
    Expected output
    1
    
  2. Example 2

    Input
    1 10
    
    Expected output
    4
    
  3. Example 3

    Input
    1 1000000000000000000
    
    Expected output
    83