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
akababa: 2017-04-26 07:25:00

How would you even use pre-calculated valuess... lol

visu27: 2017-03-23 10:21:21

easy one AC in one go

stark_attack: 2017-02-23 11:34:14

very easy , with just two loops .AC in first go .....

cake_is_a_lie: 2017-02-11 00:37:59

Lots of ways to solve this, but for me the thought pattern was "if only I could easily find a bound on the number of iterations it might take to reach 1"...

aditya9125: 2016-12-30 11:17:27

Can be done without map or sets.Read few lines about happy numbers n you'll be able to figure out the rest.

avi892nash: 2016-12-27 20:23:09

How can we say that breaking the number would lead to a cycle or it become 1....is there is any case in which breaking of a number goes to infinity but 1 does not appear and cycle also not appear

nolex: 2016-12-13 01:51:46

Does output have to be in file or on console... If it is file, do I need one more scanf for file for output?

Last edit: 2016-12-13 19:41:18
kira28: 2016-12-08 12:38:29

LOL!!! its as easy as it looks

epsilonalpha: 2016-11-27 03:28:21

Easy, analyze the given examples.
A/C in one go using O(1) memory and 0s in C++ without recursion.
Sets and maps will work great,I solved this using Floyd's.

E Naveen Kumar: 2016-11-04 07:43:38

Please analyse carefully. Simple
AC in a Go.....


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