Created
March 4, 2022 22:12
-
-
Save cfredmond/b1c7f305ce1c90778e1c0ef6e01b5901 to your computer and use it in GitHub Desktop.
happy numbers
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
| def foo(param): | |
| cparam = param | |
| happy = False | |
| sums = [] | |
| while True: | |
| l = [int(d)**2 for d in str(param)] | |
| s = sum(l) | |
| if s == 1: | |
| happy = True | |
| break | |
| elif s in sums: # while testing i noticed that unhappy numbers created a list of repeating sumed elements. breaking out of loop if number appears | |
| break | |
| sums.append(s) | |
| param = s | |
| return { cparam : happy } | |
| print([foo(e) for e in range(100)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment