dsa · easy
Happy Number
Replace a positive integer n by the sum of the squares of its decimal digits, and repeat. n is happy if this process reaches 1. If it falls into a cycle that does not include 1, it is not happy. Return whether n is happy.
Arguments
n— positive integer to test for the happy-number process
Example
n = 19:
1² + 9² = 82 8² + 2² = 68 6² + 8² = 100 1² + 0² + 0² = 1 → true.
n = 2 cycles 4 → 16 → 37 → … → 4 and never hits 1 → false.
Constraints
1 <= n <= 2^31 - 1 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: 19 Expected: true
Example 2
Input: 2 Expected: false