How to check if a given number is a Happy Number?

Varanasi Software Junction: Disarium Number



What is a Happy Number?

(This is a question that is asked often in Coding Interviews and also in programming assignments in schools and college courses)
A Happy Number is a number where the sum of digits squared s equal to 1 ultimately. What do we mean by ultimately?
The best way is to learn by an example.
Example
10 = 12 + 02 = 1 + 0=1. So 10 is a Happy Number


Let us try with 19
sum =12 + 92 = 1 + 81 = 82
sum=82 + 22 = 64 + 4 = 68
sum=62 + 82 = 36 + 64 = 100
sum= 12 + 02 + 02= 1 + 0 + 0 = 1
19 ultimately reaches 1 so it is a Happy Number.

Let us try with 17

sum = 12 + 72 = 1 + 49 = 50
sum = 52 + 02 = 25 + 0 = 25
sum = 22 + 52 = 4 + 25 = 29
sum = 22 + 92 = 4 + 81 = 85
sum = 82 + 52 = 64 + 25 = 89
sum = 82 + 92 = 64 + 81 = 145
sum=12 + 42 + 52 = 1 + 16 + 25 = 42
sum=42 + 22 = 16 + 4 =20
sum = 22 = 4
sum = 42  = 16
sum = 12 + 62 = 1 + 36 = 37
sum=32 + 72 = 9 +49=58
sum=52 + 82 = 25 + 64 = 89
We have reached 89 once again and the cycle will repeat and hence 17 is not a Happy Number.

Steps in solving the problem.

1.  Take a number which is to classified as Happy or Not Happy
2. Break it into its individual digits.
How to do this?
We will use a combination of the 2 Python arithmetic operators // and %. // is the integer divisor and gives the result of division without the decimal part. 
S0, 12//10 = 1 is 1 and not 1.2.  Similarly 9//10 is 0 and not 0.9.

The other operator is % the remainder operator and gives the remainder when a is divided by b. Thus
7 % 2 is 1 and 7//2.is 3 while 7/2 is3.5. Applying the remainder operator with divisor = 10 will always return the last digit.

Program to find quotient and remainder in Python.



# Program to get remainder and integer quotient of an integer

n = 123
intquotient = n // 10
remainder = n % 10
print("Quotient = ", intquotient, ", remainder = ", remainder)

# End program

Output




Quotient = 12 , remainder = 3

Process finished with exit code 0




How to find all digits of a number?

Run a loop till the number becomes 0. Take the remainder and divide by 10 to remove 1 digit at a time.


# Program to find all digits of a number

n = 123
while n > 0:
rem = n % 10
n //= 10
print(rem)

# End program

Output




3
2
1

Process finished with exit code 0


The next job is to find the sum of squares of all the digits.



# Program to find all digits of a number

n = 123
sum = 0
while n > 0:
rem = n % 10
n //= 10
sum += rem * rem

print(sum)

# End program

Output



14

Process finished with exit code 0

 We are ready to provide the final solution now. We will run an infinite loop in which we will find the sum of the digits. If the sum is 1 then we declare it a Happy Number. If the sum isn't 1 then we add the sum to a list. Using the is operator we check if the number has been found before. If yes then declare it a non Happy Number. If no, then assign the sum to n and continue the loop.


Program to search for a Happy Number



# Program to search for Happy Numbers


"""
A Happy Number is a number where the sum of digits squared s equal to 1 ultimately. What do we mean by ultimately?
The best way is to learn by an example.
Example
10 = 12 + 02 = 1 + 0=1. So 10 is a Happy Number

Let us try with 19
sum =12 + 92 = 1 + 81 = 82
sum=82 + 22 = 64 + 4 = 68
sum=62 + 82 = 36 + 64 = 100
sum= 12 + 02 + 02= 1 + 0 + 0 = 1
19 ultimately reaches 1 so it is a Happy Number.


"""

n = 8088
ncopy = n
a = [n]
while True:
sum = 0
while n > 0:
rem = n % 10
n = n // 10
sum += rem * rem
if sum == 1:
a += [sum]
print(ncopy, " is a happy number")
print(a)
break
if sum in a:
a = a + [sum]
print(ncopy, " is not a happy number")
print(a)
break
a = a + [sum]
n = sum

# End Program

Output



8088 is a happy number
[8088, 192, 86, 100, 1]

Process finished with exit code 0


Program


# Program to search for Happy Numbers


"""
A Happy Number is a number where the sum of digits squared s equal to 1 ultimately. What do we mean by ultimately?
The best way is to learn by an example.
Example
10 = 12 + 02 = 1 + 0=1. So 10 is a Happy Number

Let us try with 19
sum =12 + 92 = 1 + 81 = 82
sum=82 + 22 = 64 + 4 = 68
sum=62 + 82 = 36 + 64 = 100
sum= 12 + 02 + 02= 1 + 0 + 0 = 1
19 ultimately reaches 1 so it is a Happy Number.


"""

n = 18
ncopy = n
a = [n]
while True:
sum = 0
while n > 0:
rem = n % 10
n = n // 10
sum += rem * rem
if sum == 1:
a += [sum]
print(ncopy, " is a happy number")
print(a)
break
if sum in a:
a = a + [sum]
print(ncopy, " is not a happy number")
print(a)
break
a = a + [sum]
n = sum

# End Program


Output



18 is not a happy number
[18, 65, 61, 37, 58, 89, 145, 42, 20, 4, 16, 37]

Process finished with exit code 0


Further tasks. Change this program so that it finds all the Happy Numbers up to 1000.

Contact us for software training, education or development










 
















Post a Comment

0 Comments