📘
Happy Number
EasyHash Maps & Sets
A happy number is defined by repeatedly replacing it with the sum of squares of its digits until it either reaches 1 (happy) or loops endlessly (not happy).
Return True if n is a happy number.
Example:
Input:
19
Output:
True
Reasoning:
- We start with the input number 19 and calculate the sum of squares of its digits: 12+92=1+81=82
- Then, we repeat the process with 82: 82+22=64+4=68
- Next, we calculate the sum of squares of the digits of 68: 62+82=36+64=100
- Finally, we continue this process: 12+02+02=1, which is 1, so the number 19 is happy, and we return True
Constraints:
- 1 <= n <= 2^31 - 1
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.