HPYNOSII - Happy Numbers II

The process of “breaking” an integer is defined as summing the squares of its digits. For example, the result of breaking the integer 125 is (12 + 22 + 52) = 30. An integer N is happy if after “breaking” it repeatedly the result reaches 1. If the result never reaches 1 no matter how many times the “breaking” is repeated, then N is not a happy number.

Task

Write a program that given an integer T (number of test cases) and T integers, determines for each number whether it is a happy number or not.

Constraints

1 ≤ T ≤ 1,080,000

2 ≤ N ≤ 2,147,483,647 (number for determining whether it is happy or not)

Input

The first line contains an integer T.

Next T lines contain an integer N for detemining whether it is happy or not.

Output

T lines containing a single integer N which is the number of times the process had to be done to determine that N is happy, or -1 if N is not happy.

Example

Input:
2
19
204

Output:
4
-1

Explanation

First test case:

  • 19 : 12 + 92 = 82
  • 82 : 82 + 22 = 68
  • 68 : 62 + 82 = 100
  • 100 : 12 + 02 + 02 = 1

The solution for 19 is 4 because we discovered that the integer 19 is happy after we repeated the process 4 times.

Second test case:

204 → 20 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 → 145 ...

204 is not a happy number because after breaking it several times the results start repeating so we can deduce that if we continue breaking it, the result will never reach 1.


Added by:Rofael Emil
Date:2010-11-05
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:(modified) Egyptian Olympiad in Informatics ( EOI ) 2009, August 14 - 21, Cairo

hide comments
2012-04-04 21:41:37 axe_it
i goodled how to take fast input for this problem.. and in c++ we use readint and
fread(buf, 1, MAX - 1, stdin); whwre buff is an array and max is the size

Last edit: 2012-04-04 21:42:41
2012-04-04 19:58:39 Nilendu Das
gets() is much faster than scanf("%s")..
Try to use it wisely..
2011-07-01 23:23:02 Somnath Singh :D
I have used a little OS concept , Previouly I was using function calls for calculating the squares of the digits , Instead of calling the function better to do it in the main(). Becoz it will take time to stack & unstack . Be caution using printf or puts.

With This optimizaition I finally got through .... Phew !!!!
2011-04-28 06:05:29 Santiago Palacio
Buda, which IO did you use?
2011-04-23 23:29:31 Buda IM (retired)
Same algo
scanf : 5.57s ;;
optimized IO 1.55
2010-12-13 22:07:20 hendrik
Even though people complained a little about the timeout at the beginning finally it turned out to be a fair limit.
2010-12-13 22:07:20 যোবায়ের
Instead of tightening time limit, how about tightening source limit... Then I am not going to be able to solve it :D

Last edit: 2010-11-14 22:20:25
2010-12-13 22:07:20 Rofael Emil
@hendrik ---> navigate to "Happy Numbers II - Trial" in "Tutorial" section
@ https://www.spoj.pl/problems/HPYNOSI
2010-12-13 22:07:20 hendrik
Could be an idea to create a copy of this with much larger timeout in the tutorial section. Both I/O and algo matter. So people could experiment a little.
2010-12-13 22:07:20 LeppyR64
@naive_coder: Yes, I did.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.