Skip to content

Instantly share code, notes, and snippets.

@rodrigoSyscop
Created May 31, 2017 14:13
Show Gist options
  • Save rodrigoSyscop/bf19cdefb97aa728103c58556f2d33a4 to your computer and use it in GitHub Desktop.
Save rodrigoSyscop/bf19cdefb97aa728103c58556f2d33a4 to your computer and use it in GitHub Desktop.
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