Created
March 3, 2016 10:59
-
-
Save davidbj/02ff85f36bddd4f87519 to your computer and use it in GitHub Desktop.
python happy number demo.
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 happy_num(num): | |
| st = set() | |
| while True: | |
| if num == 1: | |
| return True | |
| num = sum([int(i)**2 for i in str(num)]) | |
| if num in st: | |
| return False | |
| st.add(num) | |
| def fn(): | |
| happy_num_lst = set() | |
| for i in range(10000): | |
| if happy_num(i): | |
| happy_num_lst.add(i) | |
| return happy_num_lst | |
| fn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment