Python Program to Print all Happy Numbers Between 1 and 100
Below, are the code examples of Python programs to Print all Happy Numbers Between 1 and 100.
- Using Set() Method
- Using Floyd’s Algorithm
- Using Recursion
Print all Happy Numbers Between 1 and 100 Using Set() Method
In this example, below Python code defines two functions: ‘numSquareSum()’ function computes the sum of squares of digits. ‘isHappyNumber()‘ uses ‘numSquareSum()‘ to determine happiness, tracking visited numbers with a set. The loop prints Happy Numbers between 1 and 100 using ‘isHappyNumber()’.
# Method returns true if n is a Happy Number
def numSquareSum(n):
num = 0
while(n):
digit = n % 10
num = num + digit*digit
n = n // 10
return num
def isHappyNumber(n):
st = set()
while (1):
n = numSquareSum(n)
if (n == 1):
return True
if n in st:
return False
st.add(n)
# Print all Happy Numbers between 1 and 100
for i in range(1, 101):
if isHappyNumber(i):
print(i, end=" ")
Output
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
Print all Happy Numbers Between 1 and 100 Using Floyd’s Cycle Detection Algorithm
In this example, in below Python code loop checks numbers from 1 to 100 using ‘isHappyNumber()‘, which applies Floyd’s Cycle Detection Algorithm with ‘numSquareSum()‘ to find happy numbers. ‘numSquareSum()‘ calculates the sum of squares of digits.
# Utility method to return
# sum of square of digits of n
def numSquareSum(n):
squareSum = 0
while n:
squareSum += (n % 10) * (n % 10)
n = n // 10
return squareSum
# Method returns true if n is a Happy number
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
# Print all Happy Numbers between 1 and 100
for i in range(1, 101):
if isHappyNumber(i):
print(i, end=" ")
Output
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
Print all Happy Numbers Between 1 and 100 Using Recursion
In this example, Python code defines two functions: ‘numSquareSum()‘ calculates the sum of squares of digits of a number, and ‘isHappyNumber()‘ determines if a number is happy by recursively applying ‘numSquareSum()’ until either 1 is reached (indicating happiness) or a cycle is detected, using a set to keep track of visited numbers.
# Utility method to return sum of square of digits of n
def numSquareSum(n):
squareSum = 0
while n:
squareSum += (n % 10) * (n % 10)
n = n // 10
return squareSum
# Method returns true if n is a Happy number
def isHappyNumber(n, visited):
if n == 1:
return True
elif n in visited:
return False
else:
visited.add(n)
return isHappyNumber(numSquareSum(n), visited)
# Print all Happy Numbers between 1 and 100
for i in range(1, 101):
visited = set()
if isHappyNumber(i, visited):
print(i, end=" ")
Output
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
Python Program to Print all Happy Numbers Between 1 and 100
A Happy Number n is defined by the following process. Starting with n, replace it with the sum of the squares of its digits, and repeat the process until n equals 1, or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are Happy Numbers, while those that do not end in 1 are unhappy numbers.
Example :
Input: n = 19
Output: TrueExplanation
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.