Create happy_number.py

This commit is contained in:
bt3gl 2023-07-30 13:04:04 -07:00 committed by GitHub
parent 8d6fac8dc9
commit 48ce09742e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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