mirror of
https://github.com/c0de-archive/hacktoberfest-2018.git
synced 2025-07-30 13:30:16 +00:00
github.com/sadikkuzu
This commit is contained in:
23
code/sadikkuzu.py
Normal file
23
code/sadikkuzu.py
Normal file
@@ -0,0 +1,23 @@
|
||||
## https://gist.github.com/sadikkuzu/af6a69d73f463b1b6b30aec15935dbc9
|
||||
# https://www.wikiwand.com/en/Happy_number
|
||||
# http://mathworld.wolfram.com/HappyNumber.html
|
||||
|
||||
|
||||
def square(x):
|
||||
return int(x) * int(x)
|
||||
|
||||
|
||||
def happy(number):
|
||||
return sum(map(square, list(str(number))))
|
||||
|
||||
|
||||
def is_happy(number):
|
||||
seen_numbers = set()
|
||||
while number > 1 and (number not in seen_numbers):
|
||||
seen_numbers.add(number)
|
||||
number = happy(number)
|
||||
return number == 1
|
||||
|
||||
|
||||
def is_happy2(n): # recursive
|
||||
return (n == 1 or n > 4 and is_happy2(happy(n)))
|
Reference in New Issue
Block a user