UNHAPPY - Unhappy Numbers

no tags 

Numbers have feelings too! For any positive integer, take the sum of the squares of each of its digits, and add them together. Take the result, and do it again. A number is Happy if, after repeating this process a finite number of times, the sum is 1. Some happy numbers take more iterations of this process to get to 1 than others, and that would be referred to as its distance from happiness. 1’s distance from happiness is 0. 23’s distance from happiness is 3, since 22 + 32 = 13, 12 + 32 = 10, and 12 + 02 = 1. Numbers are unhappy if they are infinitely far away from happiness because they get stuck in a loop.

Given the lower end and upper end of a range of integers, determine how many Unhappy numbers are in that range (inclusive).

Input

There will be several test cases in the input. Each test case will consist of two positive integers, lo and hi (0 < lohi ≤ 1018) on a single line, with a single space between them. Input will terminate with two 0s.

Output

For each test case, output a single integer on its own line, indicating the count of Unhappy Numbers between lo and hi (inclusive). Output no extra spaces, and do not separate answers with blank lines.

Example

Input:
1 10
1 100
0 0

Output:
7
80

hide comments
xncrpt: 2016-12-28 22:28:36

Great Problem .. :D

ashishranjan28: 2016-10-29 20:36:00

solved after 1 month... after learning digit dynamic programming

Last edit: 2016-10-29 20:36:12

Added by:Joshua Kirstein
Date:2014-11-01
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU
Resource:ACM SER2012