Created
May 31, 2017 14:13
-
-
Save rodrigoSyscop/bf19cdefb97aa728103c58556f2d33a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
history = [] | |
def happy(number): | |
next_ = sum(int(n) ** 2 for n in str(number)) | |
# stop condition | |
if next_ == 1: | |
return True | |
elif next_ in history: | |
return False | |
history.append(next_) | |
return happy(next_) | |
assert happy(1) | |
assert happy(10) | |
assert happy(100) | |
assert happy(130) | |
assert not happy(123) | |
assert not happy(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment