Created
September 22, 2016 23:54
-
-
Save lucasrcezimbra/bcce56db98e5cf2cd921ddb4b6da169d to your computer and use it in GitHub Desktop.
Happy Numbers
This file contains 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
def happy(number): | |
if number < 10: | |
return number in (1,7) | |
next_ = sum(int(char) ** 2 for char in str(number)) | |
return happy(next_) | |
assert all([happy(n) for n in (1, 10, 100, 130, 97)]) | |
assert not all([happy(4) for n in (2,3,4,5,6,8,9)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment