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)!!!


hide comments
aditya1997: 2016-02-09 00:14:30

set.....the key to AC :)

Aqib Ahmed J: 2016-01-31 07:48:11

Tick mark in one go :)

Parikshith Rai: 2016-01-23 07:39:30

got 3 nzecs only because i thought that there are 32 test cases.
haha..well,now I'm embarrassed :D

AdityaAnand: 2016-01-19 10:35:57

recursion takes its toll

prateekmathur1991: 2016-01-12 12:05:42

AC in 1 Go with Java.... :) Pay close attention to the reason why 204 is not happy! :)

bhavya singh: 2016-01-07 18:30:49

32nd input file ??!!
plz help :(

sarthak_8: 2016-01-03 20:27:41

@Aashish Kumar did you use a check from the pattern or you cheked for a cycle? I got an AC by using the check from pattern. Will that be considered cehating accroding to the last line? :P

Last edit: 2016-01-03 20:28:28
AASHISH KUMAR: 2015-12-29 10:12:46

solve atleast 8 to 10 numbers you will get your pattern and logic :D

PS: It took me 15 test case to figure it out slow processor :( :D

Last edit: 2016-01-13 13:47:31
Junaid: 2015-12-22 09:26:06

Can anyone have a look on my code and tell me what's the problem in this code why it's giving WA.....Thanks in advance....here's the link
[code removed]
Finally AC after messing up with the problem for more than two hours...;)

Last edit: 2015-12-22 15:43:08
killjee_15: 2015-12-16 20:03:49

Using Arrays Costed Me 2 SIGSEGV


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