thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

ByteByteGo: Happy Number

β€’ 54 words β€’ 1 min β€’ updated

ByteByteGo: Happy Number:

python
def happy_number(n: int) -> bool:
    seen = (set([n]))

    def apply(n):
        ans = 0

        while n > 0:
            ans += (n % 10) ** 2
            n //= 10

        return ans

    assert apply(23) == 13

    while n != 1:
        n = apply(n)

        if n in seen:
            return False
        else:
            seen.add(n)

    return True