Happy Number
In this article, we will explore the concept of happy numbers and learn how to determine whether a given number is happy or not. We will provide an explanation of the problem, present a suitable example, outline the algorithm, provide pseudocode, and explain the resultant output with its time complexity.
Introduction
A happy number is a positive integer defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 or gets stuck in a cycle that does not include 1. If the process ends in 1, then the number is considered happy; otherwise, it is an unhappy number.
Problem Statement
The problem is to determine whether a given number is happy or not. Given a positive integer, we need to apply the process of summing the squares of its digits repeatedly until we reach either 1 or a cycle that does not include 1. If the process ends in 1, the number is considered happy; otherwise, it is an unhappy number.
For example, let's consider the number 19:
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
Since the process ends in 1, the number 19 is a happy number.
Algorithm
To determine if a given number is happy, we can follow these steps:
- Create a function to calculate the sum of the squares of the digits of a given number.
- Create a function to check if a number is happy or not.
- Inside the "is_happy_no" function, initialize a variable with the given number.
- Use a loop to repeatedly calculate the sum of the squares of the digits until the number becomes either 1 or 4.
- If the number becomes 1, print that it is a happy number.
- If the number becomes 4, print that it is an unhappy number.
Pseudocode
function digit_square(number):
result = 0
while number is not equal to 0:
digit = number % 10
result += digit * digit
number = number / 10
return result
function is_happy_no(value):
number = value
while number is not equal to 4 and number is not equal to 1:
number = digit_square(number)
if number is equal to 1:
print value, "is a Happy Number"
else:
print value, "is an Unhappy Number"
is_happy_no(31)
is_happy_no(21)
is_happy_no(44)
Code Solution
Here given code implementation process.
//C Program
//Check if a given number is happy or not
#include <stdio.h>
//This method returns the sum of the digits of the given number.
long digit_square(long number)
{
long result = 0;
long digit = 0;
while (number!=0)
{
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
void is_happy_no(long value)
{
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while(number!=4 && number!=1)
{
number=digit_square(number);
}
if(number==1)
{
printf(" %ld Is Happy Number\n",value);
}
else
{
printf(" %ld Is UnHappy Number\n",value);
}
}
int main() {
is_happy_no(31);
is_happy_no(21);
is_happy_no(44);
return 0;
}
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
/*
C++ Program
Check if a given number is happy or not
*/
#include<iostream>
using namespace std;
class MyNumber {
public:
//This method returns the sum of the digits of the given number.
long digit_square(long number) {
long result = 0;
long digit = 0;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit *digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
void is_happy_no(long value) {
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while (number != 4 && number != 1) {
number = this->digit_square(number);
}
if (number == 1) {
cout << value << " Is Happy Number\n";
} else {
cout << value << " Is UnHappy Number\n";
}
}
};
int main() {
MyNumber obj ;
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
return 0;
}
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
/*
Java Program
Check if a given number is happy or not
*/
public class MyNumber {
//This method returns the sum of the digits of the given number.
public long digit_square(long number)
{
long result = 0;
long digit = 0;
while (number!=0)
{
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
public void is_happy_no(long value)
{
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while(number!=4 && number!=1)
{
number=digit_square(number);
}
if(number==1)
{
System.out.print(value+" Is Happy Number\n");
}
else
{
System.out.print(value+" Is UnHappy Number\n");
}
}
public static void main(String[] args) {
MyNumber obj = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
}
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
/*
C# Program
Check if a given number is happy or not
*/
using System;
public class MyNumber {
//This method returns the sum of the digits of the given number.
public long digit_square(long number) {
long result = 0;
long digit = 0;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
public void is_happy_no(long value) {
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while (number != 4 && number != 1) {
number = digit_square(number);
}
if (number == 1) {
Console.Write(value + " Is Happy Number\n");
} else {
Console.Write(value + " Is UnHappy Number\n");
}
}
public static void Main(String[] args) {
MyNumber obj = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
}
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
# Python 3 Program
# Check if a given number is happy or not
class MyNumber :
#This method returns the sum of the digits of the given number.
def digit_square(self, number) :
result = 0
digit = 0
while (number != 0) :
#Get last digit
digit = number % 10
#Squre and sum of digit
result += digit * digit
#Remove last digit
number = int(number / 10)
return result
#Method which is determine whether given number is happy number or not
def is_happy_no(self, value) :
number = value
# When number is 4 or digit square result is 4 that means no is an not happy number
# Test Case when number is 1 that means sequece digit are valid
while (number != 4 and number != 1) :
number = self.digit_square(number)
if (number == 1) :
print(value ," Is Happy Number")
else :
print(value ," Is UnHappy Number")
def main() :
obj = MyNumber()
# Test Case
obj.is_happy_no(31)
obj.is_happy_no(21)
obj.is_happy_no(44)
if __name__ == "__main__":
main()
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
# Ruby Program
# Check if a given number is happy or not
class MyNumber
#This method returns the sum of the digits of the given number.
def digit_square(number)
result = 0
digit = 0
while (number != 0)
#Get last digit
digit = number % 10
#Squre and sum of digit
result += digit * digit
#Remove last digit
number /= 10
end
return result
end
#Method which is determine whether given number is happy number or not
def is_happy_no(value)
number = value
# When number is 4 or digit square result is 4 that means no is an not happy number
# Test Case when number is 1 that means sequece digit are valid
while (number != 4 and number != 1)
number = self.digit_square(number)
end
if (number == 1)
print(value ," Is Happy Number\n")
else
print(value ," Is UnHappy Number\n")
end
end
end
def main()
obj = MyNumber.new()
# Test Case
obj.is_happy_no(31)
obj.is_happy_no(21)
obj.is_happy_no(44)
end
main()
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
/*
Scala Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
def digit_square(value: Long): Long = {
var result: Long = 0;
var digit: Long = 0;
var number: Long = value;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
def is_happy_no(value: Long): Unit = {
var number: Long = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while (number != 4 && number != 1) {
number = this.digit_square(number);
}
if (number == 1) {
print(s"$value Is Happy Number\n");
} else {
print(s"$value Is UnHappy Number\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
}
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
/*
Swift 4 Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
func digit_square(_ value: Int) -> Int {
var result: Int = 0;
var digit: Int = 0;
var number: Int = value;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
func is_happy_no(_ value: Int) {
var number: Int = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while (number != 4 && number != 1) {
number = self.digit_square(number);
}
if (number == 1) {
print(value ," Is Happy Number");
} else {
print(value ," Is UnHappy Number");
}
}
}
func main() {
let obj: MyNumber = MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
main();
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
<?php
/*
Php Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
public function digit_square($number) {
$result = 0;
$digit = 0;
while ($number != 0) {
//Get last digit
$digit = $number % 10;
//Squre and sum of digit
$result += $digit *$digit;
//Remove last digit
$number = intval( $number / 10);
}
return $result;
}
//Method which is determine whether given number is happy number or not
public function is_happy_no($value) {
$number = $value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while ($number != 4 && $number != 1) {
$number = $this->digit_square($number);
}
if ($number == 1) {
echo($value ." Is Happy Number\n");
} else {
echo($value ." Is UnHappy Number\n");
}
}
};
function main() {
$obj = new MyNumber();
// Test Case
$obj->is_happy_no(31);
$obj->is_happy_no(21);
$obj->is_happy_no(44);
}
main();
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
/*
Node Js Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
digit_square(number) {
var result = 0;
var digit = 0;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit *digit;
//Remove last digit
number = parseInt(number / 10);
}
return result;
}
//Method which is determine whether given number is happy number or not
is_happy_no(value) {
var number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no is an not happy number
while (number != 4 && number != 1) {
number = this.digit_square(number);
}
if (number == 1) {
process.stdout.write(value + " Is Happy Number\n");
} else {
process.stdout.write(value + " Is UnHappy Number\n");
}
}
}
function main(args) {
var obj = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44)
}
main();
Output
31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
Explanation and Result
Let's go through the given code and understand how it determines whether a number is happy or unhappy.
The code starts with defining the "digit_square" function, which calculates the sum of the squares of the digits of a given number. It uses a while loop to extract each digit from the number, square it, and add it to the result. The last digit is removed in each iteration by dividing the number by 10.
The "is_happy_no" function is responsible for determining whether a number is happy or not. It takes a value as an input and initializes a variable "number" with that value. It then uses a while loop to repeatedly calculate the sum of the squares of the digits until the number becomes either 1 or 4. If the number becomes 1, it prints that the number is happy; otherwise, it prints that the number is unhappy.
The main function calls the "is_happy_no" function for three different numbers: 31, 21, and 44.
The output of the code is as follows:
31 is a Happy Number
21 is an Unhappy Number
44 is a Happy Number
The code correctly identifies 31 and 44 as happy numbers, as their process of summing the squares of the digits leads to 1. On the other hand, 21 is identified as an unhappy number because its process does not reach 1 and gets stuck in a cycle.
Time Complexity
The time complexity of the code is dependent on the number of digits in the given number. Let's denote the number of digits as "n".
The "digit_square" function has a while loop that iterates once for each digit in the number. Therefore, its time complexity is O(n).
The "is_happy_no" function also has a while loop that depends on the number of iterations until the number becomes either 1 or 4. In the worst case, the number can cycle indefinitely, but for most inputs, it reaches 1 after a few iterations. Therefore, the time complexity can be considered as O(k), where "k" is the number of iterations until the number reaches 1 or 4.
Overall, the time complexity of the code is O(n) + O(k). However, since the number of digits "n" and the number of iterations "k" are generally small for most inputs, the code can be considered efficient.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment