We use a hash set to detect cycles. If we encounter a sum that we've seen before and it's not 1, the number will never reach 1.
- Initialize
seen = set(). - While
n != 1andnnot inseen:seen.add(n).- Calculate sum of squares of digits of
n. - Update
nwith this sum.
- Return
n == 1.
- Time Complexity: O(log N) (finding digits and summing squares).
- Space Complexity: O(log N) (to store seen numbers).
def is_happy(n):
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = sum(int(digit) ** 2 for digit in str(n))
return n == 1