A happy number is defined as:
A number which, when replaced by the sum of the digits squared and repeated infinitely, will equal in a series of ‘1’
The operation to detect a happy number is:
- Split the individual digits of a number (eg. 523 becomes 5,2 and 3)
- Square each of these number (25,4 and 9)
- Add these squares together (25+4+9=38)
- Is your new number equal to 1? If so you have a happy number else return to step 1 with your newly calculated number
If your number produces an infinitely recurring loop of numbers that do not result in 1 then your number is not a happy number and therefore unhappy.
293 Is a happy number
And an example of an unhappy number:
294 is not a happy number
In the second example we see that 294 is not a happy number because it produces a recurring loop: 4,16,37,58,89,145,42,20. This is the standard unhappy loop, once you have any of these numbers your number must be unhappy as this will cause an infinite loop.