Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Happy Number
Photo Credit to CodeToFun
π Introduction
In the domain of programming, certain mathematical properties and patterns are often explored. One such interesting concept is the notion of "happy numbers."
In this tutorial, we'll delve into a Python program designed to determine whether a given number is a happy number.
π Example
Let's take a look at the Python code that checks whether a given number is a happy number or not.
# Function to calculate the sum of squares of digits
def sum_of_squares(n):
sum_val = 0
while n > 0:
digit = n % 10
sum_val += digit * digit
n //= 10
return sum_val
# Function to check if a number is a happy number
def is_happy_number(n):
slow = n
fast = n
while True:
slow = sum_of_squares(slow)
fast = sum_of_squares(sum_of_squares(fast))
if slow == fast:
break
return slow == 1
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 19
# Check if the number is a happy number
if is_happy_number(number):
print(f"{number} is a Happy Number.")
else:
print(f"{number} is not a Happy Number.")
π» Testing the Program
To test the program with a different number, replace the value of number in the if __name__ == "__main__": block.
19 is a Happy Number.
Run the script to check if the number is a happy number.
π§ How the Program Works
- The program defines a function sum_of_squares to calculate the sum of the squares of digits of a given number.
- It then defines a function is_happy_number that checks whether a given number is a happy number using Floyd's cycle detection algorithm.
- The if __name__ == "__main__": block tests the program with a sample number (in this case, 19).
π Between the Given Range
Let's take a look at the python code that checks for Happy Numbers in the specified range.
def is_happy_number(num):
seen = set()
while num != 1 and num not in seen:
seen.add(num)
num = sum(int(digit) ** 2 for digit in str(num))
return num == 1
# Identify Happy Numbers in the range 1 to 50
happy_numbers = [num for num in range(1, 51) if is_happy_number(num)]
# Print the results
print("Happy numbers in the range 1 to 50:")
for num in happy_numbers:
print(num, end=" ")
π» Testing the Program
Happy numbers in the range 1 to 50: 1 7 10 13 19 23 28 31 32 44 49
Simply run the script to see the Happy Numbers in the range of 1 to 50.
π§ How the Program Works
- The program defines a function is_happy_number that checks whether a given number is a Happy Number.
- Inside the function, it uses a while loop to iterate through the Happy Number sequence until reaching 1 or encountering a previously seen number.
- The main section identifies and prints the Happy Numbers in the specified range (1 to 50).
π§ Understanding the Concept of Happy Number
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 it loops endlessly in a cycle that does not include 1.
For example, let's take the number 19:
- 12+92 = 82
- 82+22 = 68
- 62+82 = 100
- 12+02+02 = 1
In this case, 19 is a happy number because the process ends with 1.
π’ Optimizing the Program
While the provided program is effective, there are various ways to optimize and enhance it further. Consider exploring different algorithms or incorporating additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Happy Number), please comment here. I will help you immediately.