Created
April 21, 2020 12:47
-
-
Save overloadedargs/c83a00078dc3017666f2672618de8f40 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 is_happy(n): | |
while (n != 1): | |
digits = str(n) | |
sum = 0 | |
i = 0 | |
print n | |
while (i < len(digits)): | |
sum += int(digits[i]) ** 2 | |
i += 1 | |
n = sum | |
if (n == 1): | |
return True | |
print is_happy(7) | |
print is_happy(19) | |
print is_happy(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment