Problem statement

https://binarysearch.com/problems/Happy-Numbers/

Solution

Equal to Leetcode 0202 Happy Number.

Complexity

It is O(log n) for time and space.

Code

class Solution:
    def solve(self, n):
        d = set()
        while True:
            n2 = sum(int(dig)**2 for dig in str(n))
            if n2 == 1: return True
            if n2 in d: return False
            d.add(n2)
            n = n2