diff --git a/math_logic_dp/happy_number.py b/math_logic_dp/happy_number.py new file mode 100644 index 0000000..5e91765 --- /dev/null +++ b/math_logic_dp/happy_number.py @@ -0,0 +1,19 @@ +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 +