mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create happy_number.py
This commit is contained in:
parent
8d6fac8dc9
commit
48ce09742e
19
math_logic_dp/happy_number.py
Normal file
19
math_logic_dp/happy_number.py
Normal 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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user