HPYNOS - Happy Numbers I

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 N, determines whether it is a happy number or not.

CONSTRAINTS

2 ≤ N ≤ 2,147,483,647

Input

A single line containing a single integer N.

Output

A single line containing a single integer T 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:
19

Output:
4
1) 19  : 12 + 92 = 82
2) 82  : 82 + 22 = 68
3) 68  : 62 + 82 = 100
4) 100 : 12 + 02 + 02 = 1

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

Input:
204

Output:
-1
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.

Number of input files is 32.

Don't use pre-calculated values (Don't Cheat)!!!


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

hide comments
2015-03-02 07:43:01 Ankit
Sillly mistakes ... ;-(
2015-02-23 19:04:29 Tanmay
There are not 32 cases.
Write program for 1 input only.

Good Luck :)
2015-02-07 16:33:47 pooh
green in one go ;)
2015-01-29 16:09:53 Rohit
Got ac in first time
2015-01-17 08:13:53 Gaurav chandel
WA in 32nd test case.. dont know why
2015-01-16 10:22:43 Shekhar Prasad Rajak
Got AC in first time ..\m/
2015-01-07 05:27:48 Ankur Singh
@admin, i am trying to submit in c++(4.3.2) and it is giving error that :"language available only on cube cluster"
I don't know what that means please help!
2015-01-06 10:36:39 deerishi
The test cases are FILES!!!!!!!!!!!!!!!!!
2014-12-29 22:33:50 AlphaDecay
"Number of test cases is 32."

This line is very misleading. Please remove it.

--ans(Francky)--> I change a little the description, with in place "Number of input files is 32."

Last edit: 2014-12-29 23:20:06
2014-10-04 20:54:59 sivanatarajan
why i got wrong answer in 32nd test case pls hlp me?

[reply by cyclops: The judge runs all data sets and only tells you the result afterward. You probably failed on an early test file.]

Last edit: 2014-10-05 02:10:14
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.