Algorithm
Problem link- https://www.spoj.com/problems/HPYNOS/
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
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 &
Try With Live Editor
Input
Output
#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 &
Try With Live Editor
Input
Output
#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){
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
// int t = in.readInt();
Solver solveA = new Solver();
// while(t-- > 0){
solveA.solve(in, out);
// }
out.flush();
out.close();
}
}
class Solver{
final int mod = (int)1e9+7;
void solve(InputReader in, OutputWriter out){
long n = in.readLong();
long count = 0;
HashSet<Long> set = new HashSet<>();
while(n!=1){
set.add(n);
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);
}
}
}
class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} 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()
{
return readString();
}
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 &
Try With Live Editor
Input
Output
Demonstration
SPOJ Solution-Happy Numbers I-Solution in C, C++, Java, Python