master-algorithms-py/math/happy_number.py
bt3gl a85ed914d3 👾
2023-07-30 21:40:09 -07:00

25 lines
337 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def get_next(n):
total_sum = 0
while n > 0:
n, digit = divmod(n, 10)
total_sum += digit**2
return total_sum
def is_happy(self, n: int) -> bool:
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = get_next(n)
return n == 1