
Photo by Amine M’Siouri from Pexels
If we take a number, and sum the square of its digits, and then continue this process with the new number, we will either arrive at 1, or we will loop endlessly in a cycle that repeats itself. Those numbers that eventually end up at 1 are called happy numbers.
Consider the number 19. If we square its digits and then add them together, we get:
12 + 92 = 82
Then again:
82 + 22 = 68
Continuing in this fashion, we get:
62 + 82 = 100
And Finally:
12 + 02 + 02 = 1.
Therefore we can say that 19 is a happy number. Now you know who you are inviting for your next party.
So, we want to write a function to check for happy numbers. Here is what the function will do in a nutshell:
- Receive a number and turn it into string
- Sum the square of the digits of the number
- If the resulting number is equal to 1, then it is a happy number.
- Else if we have not seen the result before (no cycle has formed) continue the process
- If a cycle is detected, then break the loop. The number is a sad number.
[pastacode lang=”python” manual=”%22%22%22Econowmics.com%22%22%22%0A%0A%23Happy%20numbers%0Adef%20happy(n)%3A%0A%20%20%20%20%22%22%22The%20function%20receives%20a%20number%20and%20returns%20TRUE%20if%20it%20is%20a%20happy%20number.%22%22%22%0A%0A%20%20%20%20%23Turning%20the%20number%20to%20string%0A%20%20%20%20number%20%3D%20str(n)%0A%0A%20%20%20%20%23container%0A%20%20%20%20container%20%3D%20%5B%5D%0A%0A%20%20%20%20%23running%20a%20for%20loop%20to%20square%20each%20digit%0A%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20%23temporary%20variable%2C%20need%20to%20be%20set%20zero%20in%20each%20loop%0A%20%20%20%20%20%20%20%20temp%20%3D%200%0A%20%20%20%20%20%20%20%20%23add%20each%20digit%20squared%20to%20the%20’temp’%20variable%0A%20%20%20%20%20%20%20%20for%20digit%20in%20number%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%2B%3D%20int(digit)%20**%202%0A%0A%20%20%20%20%20%20%20%20%23After%20the%20loop%20for%20all%20digits%20has%20ended%2C%20now%20is%20temp%20equal%20to%201%3F%20(is%20it%20a%20happy%20number%3F)%0A%20%20%20%20%20%20%20%20if%20temp%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%23Or%2C%20has%20this%20number%20ever%20occured%20before%3F%20if%20so%2C%20then%20it%20is%20a%20sad%20number%0A%20%20%20%20%20%20%20%20elif%20temp%20in%20container%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%23otherwise%2C%20just%20add%20it%20to%20the%20container%20and%20set%20the%20starting%20number%20equal%20to%20the%20value%20of%20temp%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20container.append(temp)%0A%20%20%20%20%20%20%20%20%20%20%20%20number%20%3D%20str(temp)” message=”” highlight=”” provider=”manual”/]
Let us run a quick test to see how much it takes for the function to calculate how many happy numbers are therebelow 1000:
[pastacode lang=”python” manual=”%23Importing%20the%20time%20module%0Aimport%20time%0A%0A%23Marking%20the%20start%20of%20time%0Astart%3D%20time.time()%0A%0A%23Variable%20to%20store%20the%20number%20of%20happy%20numbers%0Acount%20%3D%200%0A%0A%23Loop%20to%20check%20for%20happy%20numbers%0Afor%20i%20in%20range(1%2C1001)%3A%0A%20%20%20%20if%20happy(i)%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%0A%23Marking%20the%20end%20of%20time%0Aend%20%3D%20time.time()%0A%0A%23Printing%20the%20output%20to%20the%20console%0Aprint%20(‘Total%20happy%20numbers%20below%201000%3A%20’%20%2B%20str(count))%0Aprint%20(‘It%20took%3A%20’%20%2B%20str(end-start))” message=”” highlight=”” provider=”manual”/]
Here is the result: