3) Python program to check if the given number is Happy Number
A number is said to be happy if it yields 1 when replaced by the sum of squares of its digits repeatedly. If this process results in an endless cycle of numbers containing 4, then the number will be an unhappy number.
Let's understand by an example:
Number = 32
32+ 22 = 13
12 + 32 = 10
12 + 02 = 1
In this example, we split 32 to get the sum of squares of its digits which forms another number (13), we replace 32 by 13 to continue this cycle until result 1. We found 32 a happy number.
If the above cycle for any number results in 1 then that number will be a Happy number otherwise that will be an unhappy number resulting 4, 16, 37, 58, 89, 145, 42, 20,.....
Some Happy numbers are 7, 28, 100, 320, etc.
In this program, we need to determine whether the given number is a Happy number or not by following the algorithm below:
ALGORITHM:
-
STEP 1: isHappyNumber() determines whether a given number is happy or not.
-
If the number is greater than 0, then calculate remainder rem by dividing the number with 10.
-
Calculate square of rem and add it to a variable sum.
-
Divide number by 10.
-
Repeat the steps from a to c till the sum of the square of all digits present in number has been calculated.
-
Finally, return the sum.
-
-
STEP 2: Define and initialize variable num.
-
STEP 3: Define a variable result and initialize it with a value of num.
-
STEP 4: If the result is neither equal to 1 nor 4 then, make a call to isHappyNumber().
-
STEP 5: Otherwise, if the result is equal to 1 then, given number is a happy number.
-
STEP 6: If the result is equal to 4 then, given number is not a happy number.
#isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
rem = sum = 0;
#Calculates the sum of squares of digits
while(num > 0):
rem = num%10;
sum = sum + (rem*rem);
num = num//10;
return sum;
num = 82;
result = num;
while(result != 1 and result != 4):
result = isHappyNumber(result);
#Happy number always ends with 1
if(result == 1):
print(str(num) + " is a happy number");
#Unhappy number ends in a cycle of repeating numbers which contain 4
elif(result == 4):
print(str(num) + " is not a happy number");
4)Python program to print all happy numbers between 1 and 100
In this programme, we need to print all happy numbers between 1 and 100 by following the algorithm as given below:
ALGORITHM:
-
STEP 1: isHappyNumber() determines whether a given number is happy or not.
-
If the number is greater than 0, calculate remainder rem by dividing the number with 10.
-
Calculate square of rem and add it to a variable sum.
-
Divide number by 10.
-
Repeat the steps from a to c till the sum of the square of all digits present in number has been calculated.
-
Finally, return the sum.
-
-
STEP 2: To display all happy numbers between 1 and 100,
-
Start a loop from 1 to 100, then make a call to isHappyNumber() method for each value from 1 to 100 and store the return value into a variable result.
-
If the result is neither equal to 1 nor 4 then, make a call to isHappyNumber().
-
Otherwise, if the result is equal to 1, it implies that the number is happy and display it.
-
#isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
rem = sum = 0;
#Calculates the sum of squares of digits
while(num > 0):
rem = num%10;
sum = sum + (rem*rem);
num = num//10;
return sum;
#Displays all happy numbers between 1 and 100
print("List of happy numbers between 1 and 100: ");
for i in range(1, 101):
result = i;
#Happy number always ends with 1 and
#unhappy number ends in a cycle of repeating numbers which contains 4
while(result != 1 and result != 4):
result = isHappyNumber(result);
if(result == 1):
print(i),
print(" "),