Happy Primes

No attempts yetTime limit1sMemory limit256 MB

Problem

Square every digit of a positive integer nn and add the squares together. Do the same calculation again on the sum you get. If repeating this ever produces 1, then nn is a happy number.

700 is a happy number.

  • 72+02+02=497^2 + 0^2 + 0^2 = 49
  • 42+92=974^2 + 9^2 = 97
  • 92+72=1309^2 + 7^2 = 130
  • 12+32+02=101^2 + 3^2 + 0^2 = 10
  • 12+02=11^2 + 0^2 = 1

2 is not a happy number.

  • 22=42^2 = 4
  • 42=164^2 = 16
  • 12+62=371^2 + 6^2 = 37
  • 32+72=583^2 + 7^2 = 58
  • 52+82=895^2 + 8^2 = 89
  • 82+92=1458^2 + 9^2 = 145
  • 12+42+52=421^2 + 4^2 + 5^2 = 42
  • 42+22=204^2 + 2^2 = 20
  • 22+02=42^2 + 0^2 = 4
  • 42=164^2 = 16
  • the calculation never ends.

A prime is a number with no divisors other than 1 and itself. 2, 3, 5, 7, 11, 13, 17, 19, ... are primes.

A happy prime is a number that is both prime and happy. 7, 13, 19, ... are happy primes.

Given nn, write a program that finds every happy prime less than or equal to nn.

Input

The first line contains nn (10n100000010 \le n \le 1\,000\,000).

Output

Print every happy prime less than or equal to nn in increasing order, one per line.