mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
24
math/happy_number.py
Normal file
24
math/happy_number.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
#!/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
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue