## 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.

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```

## Code Examples

### #1 Code Example with Python Programming

```Code - Python Programming```

``````def f(N):
for i in range(50):
s = 0
for j in str(N):
s += (ord(j) - 48) ** 2
if s == 1:
return i + 1
N = s
return -1

print(f(int(input())))``````
Copy The Code &

Input

cmd
19

Output

cmd
4

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````#include<iostream>
using namespace std;

unsigned int DigitsSum(unsigned long num)
{
unsigned int s=0;
short digit;
while(num)
{
digit=num%10;
s+=(digit*digit);
num/=10;
}
return s;
}
int main()
{
unsigned long n;
int cnt=0,ar[1000]={0},tmp;
cin>>n;
do{
tmp=DigitsSum(n);
cout<<tmp<<"\n";
if(ar[tmp]==0)
{
n=tmp;
ar[tmp]=1;
cnt++;
}
else{
cnt=0;
n=1;
}
}while(n!=1);
if(cnt)
cout<<cnt<<"\n";
else
cout<<"-1\n";
}``````
Copy The Code &

Input

cmd
19

Output

cmd
4

### #3 Code Example with Java Programming

```Code - Java Programming```

``````import java.io.*;
import java.util.*;
import java.math.*;

class A{
public static void main(String[] args){
OutputWriter out = new OutputWriter(System.out);
Solver solveA = new Solver();
// 		while(t-- > 0){
solveA.solve(in, out);
// 		}
out.flush();
out.close();
}
}
class Solver{
final int mod = (int)1e9+7;
long count = 0;
HashSet<Long> set = new HashSet<>();
while(n!=1){

count++;
long temp = 0;
while(n > 0){
temp += (n%10)*(n%10);
n/=10;
}
n = temp;
// out.printLine(n);
if(set.contains(n))
break;
}
if(n!=1){
out.printLine(-1);
}else{
out.printLine(count);
}

}
}
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;

{
this.stream = stream;
}

{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}

{
while (isSpaceChar(c))
int sgn = 1;
if (c == '-')
{
sgn = -1;
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
} while (!isSpaceChar(c));
return res * sgn;
}

{
while (isSpaceChar(c))
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
} while (!isSpaceChar(c));
return res.toString();
}
while (isSpaceChar(c))
int sgn = 1;
if (c == '-') {
sgn = -1;
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
}
if (c == '.') {
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
}
}
return res * sgn;
}
while (isSpaceChar(c))
int sgn = 1;
if (c == '-') {
sgn = -1;
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}

public String next()
{
}

public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}

class OutputWriter
{
private  PrintWriter writer;

public OutputWriter(OutputStream outputStream)
{
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}

public OutputWriter(Writer writer)
{
this.writer = new PrintWriter(writer);
}

public void print(Object... objects)
{
for (int i = 0; i < objects.length; i++)
{
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}

public void printLine(Object... objects)
{
print(objects);
writer.println();
}

public void close()
{
writer.close();
}

public void flush()
{
writer.flush();
}
}``````
Copy The Code &

Input

cmd
19

Output

cmd
4