Description
Determine if a number is a "happy" number.
Constraints
- Time Complexity:
O(log n) - Space Complexity:
O(log n)
Tags
hash-tablemathtwo-pointers
def isHappy(n):
slow, fast = n, sumSquareDigits(n)
while slow != fast:
fast = sumSquareDigits(fast)
fast = sumSquareDigits(fast)
slow = sumSquareDigits(slow)
return True if fast == 1 else False
def sumSquareDigits(n):
output = 0
while n:
output += (n % 10) ** 2
n = n // 10
return output