Skip to content

Instantly share code, notes, and snippets.

@DavidPu
Last active March 28, 2017 17:58
Show Gist options
  • Save DavidPu/2555a043a59ad5eec41507d1fc5bd1ed to your computer and use it in GitHub Desktop.
Save DavidPu/2555a043a59ad5eec41507d1fc5bd1ed to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
def happy_number(val):
nums = []
nums.append(val)
while True:
new_num = sum([pow(int(c), 2) for c in str(val)])
if new_num == 1:
return True
elif new_num in nums:
return False
nums.append(new_num)
val = new_num
def main():
print(happy_number(int(os.sys.argv[1])))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment