A number is called happy if it leads to 1 after a sequence of steps wherein each step number is replaced by the sum of squares of its digit that is if we start with Happy Number and keep replacing it with digits square sum, we reach 1.
Examples :
Input: n = 19
Output: True
19 is Happy Number,
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
As we reached to 1, 19 is a Happy Number.
Input: n = 20
Output: False
A number will not be a Happy Number when it makes a loop in its sequence that is it touches a number in sequence which already been touched. So to check whether a number is happy or not, we can keep a set, if the same number occurs again we flag result as not happy. A simple function on the above approach can be written as below –
C++
int isHappyNumber( int n)
{
set< int > st;
while (1)
{
n = numSquareSum(n);
if (n == 1)
return true ;
if (st.find(n) != st.end())
return false ;
st.insert(n);
}
}
|
Java
static boolean isHappyNumber( int n) {
HashSet<Integer> st = new HashSet<>();
while ( true ) {
n = numSquareSum(n);
if (n == 1 )
return true ;
if (st.contains(n))
return false ;
st.add(n);
}
}
|
C#
static int isHappyNumber( int n)
{
HashSet< int > st = new HashSet<>();
while (1)
{
n = numSquareSum(n);
if (n == 1)
return true ;
if (st.Contains(n))
return false ;
st.Add(n);
}
}
|
Javascript
<script>
let st = new Set();
while (1)
{
n = numSquareSum(n);
if (n == 1)
return true ;
if (st.has(n))
return false ;
st.add(n);
}
}
</script>
|
Python3
def isHappyNumber(n):
st = set ()
while ( 1 ):
n = numSquareSum(n)
if (n = = 1 ):
return True
if n not in st:
return False
st.insert(n)
|
Complexity Analysis:
Time Complexity: O(n*log(n)).
Auxiliary Space: O(n) since using extra set for storage
We can solve this problem without using extra space and that technique can be used in some other similar problems also. If we treat every number as a node and replacement by square sum digit as a link, then this problem is same as finding a loop in a linklist :
So as a proposed solution from the above link, we will keep two numbers slow and fast both initialize from a given number, slow is replaced one step at a time and fast is replaced two steps at a time. If they meet at 1, then the given number is Happy Number otherwise not.
C++
#include <bits/stdc++.h>
using namespace std;
int numSquareSum( int n)
{
int squareSum = 0;
while (n) {
squareSum += (n % 10) * (n % 10);
n /= 10;
}
return squareSum;
}
bool isHappynumber( int n)
{
int slow, fast;
slow = fast = n;
do {
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
} while (slow != fast);
return (slow == 1);
}
int main()
{
int n = 13;
if (isHappynumber(n))
cout << n << " is a Happy number\n" ;
else
cout << n << " is not a Happy number\n" ;
}
|
C
#include <stdbool.h>
#include <stdio.h>
int numSquareSum( int n)
{
int squareSum = 0;
while (n) {
squareSum += (n % 10) * (n % 10);
n /= 10;
}
return squareSum;
}
bool isHappynumber( int n)
{
int slow, fast;
slow = fast = n;
do {
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
} while (slow != fast);
return (slow == 1);
}
int main()
{
int n = 13;
if (isHappynumber(n))
printf ( "%d is a Happy number\n" , n);
else
printf ( "%d is not a Happy number\n" , n);
}
|
Java
class GFG {
static int numSquareSum( int n)
{
int squareSum = 0 ;
while (n!= 0 )
{
squareSum += (n % 10 ) * (n % 10 );
n /= 10 ;
}
return squareSum;
}
static boolean isHappynumber( int n)
{
int slow, fast;
slow = fast = n;
do
{
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
}
while (slow != fast);
return (slow == 1 );
}
public static void main(String[] args)
{
int n = 13 ;
if (isHappynumber(n))
System.out.println(n +
" is a Happy number" );
else
System.out.println(n +
" is not a Happy number" );
}
}
|
Python3
def numSquareSum(n):
squareSum = 0 ;
while (n):
squareSum + = (n % 10 ) * (n % 10 );
n = int (n / 10 );
return squareSum;
def isHappynumber(n):
slow = n;
fast = n;
while ( True ):
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
if (slow ! = fast):
continue ;
else :
break ;
return (slow = = 1 );
n = 13 ;
if (isHappynumber(n)):
print (n , "is a Happy number" );
else :
print (n , "is not a Happy number" );
|
C#
using System;
class GFG {
static int numSquareSum( int n)
{
int squareSum = 0;
while (n!= 0)
{
squareSum += (n % 10) *
(n % 10);
n /= 10;
}
return squareSum;
}
static bool isHappynumber( int n)
{
int slow, fast;
slow = fast = n;
do
{
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
}
while (slow != fast);
return (slow == 1);
}
public static void Main()
{
int n = 13;
if (isHappynumber(n))
Console.WriteLine(n +
" is a Happy number" );
else
Console.WriteLine(n +
" is not a Happy number" );
}
}
|
PHP
<?php
function numSquareSum( $n )
{
$squareSum = 0;
while ( $n )
{
$squareSum += ( $n % 10) *
( $n % 10);
$n /= 10;
}
return $squareSum ;
}
function isHappynumber( $n )
{
$slow ; $fast ;
$slow = $n ;
$fast = $n ;
do
{
$slow = numSquareSum( $slow );
$fast = numSquareSum(numSquareSum( $fast ));
}
while ( $slow != $fast );
return ( $slow == 1);
}
$n = 13;
if (isHappynumber( $n ))
echo $n , " is a Happy number\n" ;
else
echo n , " is not a Happy number\n" ;
?>
|
Javascript
<script>
function numSquareSum(n)
{
var squareSum = 0;
while (n!= 0)
{
squareSum += (n % 10) * (n % 10);
n = parseInt(n/10);
}
return squareSum;
}
function isHappynumber(n)
{
var slow, fast;
slow = fast = n;
do
{
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
}
while (slow != fast);
return (slow == 1);
}
var n = 13;
if (isHappynumber(n))
document.write(n +
" is a Happy number" );
else
document.write(n +
" is not a Happy number" );
</script>
|
Output :
13 is a Happy Number
Complexity Analysis:
Time Complexity: O(n*log(n)).
Auxiliary Space: O(1).
Another approach for solving this problem using no extra space.
A number cannot be a happy number if, at any step, the sum of the square of digits obtained is a single-digit number except 1 or 7. This is because 1 and 7 are the only single-digit happy numbers. Using this information, we can develop an approach as shown in the code below –
C++
#include <bits/stdc++.h>
using namespace std;
bool isHappynumber( int n)
{
if (n == 1 || n == 7)
return true ;
int sum = n, x = n;
while (sum > 9) {
sum = 0;
while (x > 0) {
int d = x % 10;
sum += d * d;
x /= 10;
}
if (sum == 1)
return true ;
x = sum;
}
if (sum == 7)
return true ;
return false ;
}
int main()
{
int n = 13;
if (isHappynumber(n))
cout << n << " is a Happy number" ;
else
cout << n << " is not a Happy number" ;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
bool isHappynumber( int n)
{
if (n == 1 || n == 7)
return true ;
int sum = n, x = n;
while (sum > 9) {
sum = 0;
while (x > 0) {
int d = x % 10;
sum += d * d;
x /= 10;
}
if (sum == 1)
return true ;
x = sum;
}
if (sum == 7)
return true ;
return false ;
}
int main()
{
int n = 13;
if (isHappynumber(n))
printf ( "%d is a Happy number" , n);
else
printf ( "%d is not a Happy number" , n);
return 0;
}
|
Java
class GFG {
static boolean isHappynumber( int n) {
if (n == 1 || n == 7 )
return true ;
int sum = n, x = n;
while (sum > 9 ) {
sum = 0 ;
while (x > 0 ) {
int d = x% 10 ;
sum += d*d;
x/= 10 ;
}
if (sum == 1 )
return true ;
x = sum;
}
if (sum == 7 )
return true ;
return false ;
}
public static void main(String[] args)
{
int n = 13 ;
if (isHappynumber(n))
System.out.println(n +
" is a Happy number" );
else
System.out.println(n +
" is not a Happy number" );
}
}
|
Python3
def isHappynumber(n):
if n = = 1 or n = = 7 :
return True
Sum , x = n, n
while Sum > 9 :
Sum = 0
while x > 0 :
d = x % 10
Sum + = d * d
x = int (x / 10 )
if Sum = = 1 :
return True
x = Sum
if Sum = = 7 :
return True
return False
n = 13
if isHappynumber(n):
print (n, "is a Happy number" )
else :
print (n, "is not a Happy number" )
|
C#
using System;
class GFG{
static bool isHappynumber( int n)
{
if (n == 1 || n == 7)
return true ;
int sum = n, x = n;
while (sum > 9)
{
sum = 0;
while (x > 0)
{
int d = x % 10;
sum += d * d;
x /= 10;
}
if (sum == 1)
return true ;
x = sum;
}
if (sum == 7)
return true ;
return false ;
}
public static void Main(String[] args)
{
int n = 13;
if (isHappynumber(n))
Console.WriteLine(n + " is a Happy number" );
else
Console.WriteLine(n + " is not a Happy number" );
}
}
|
Javascript
<script>
function isHappynumber(n)
{
if (n == 1 || n == 7)
return true ;
var sum = n, x = n;
while (sum > 9)
{
sum = 0;
while (x > 0)
{
var d = x % 10;
sum += d * d;
x /= 10;
}
if (sum == 1)
return true ;
x = sum;
}
if (sum == 7)
return true ;
return false ;
}
var n = 13;
if (isHappynumber(n))
document.write(n +
" is a Happy number" );
else
document.write(n +
" is not a Happy number" );
</script>
|
Output
13 is a Happy number
Complexity Analysis:
Time Complexity: O(n*log(n)).
Auxiliary Space: O(1).
See your article appearing on the GeeksforGeeks’ main page and help other Geeks.
Last Updated :
10 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...