28 lines
581 B
Python
Executable file
28 lines
581 B
Python
Executable file
#!/usr/bin/env python
|
|
|
|
def happynumbers_step(n):
|
|
s = 0
|
|
for c in str(n):
|
|
s += int(c)**2
|
|
return s
|
|
|
|
def happynumbers(n):
|
|
founds = []
|
|
s = n
|
|
while True:
|
|
founds.append(s)
|
|
s = happynumbers_step(s)
|
|
if s == n:
|
|
return False
|
|
elif s in founds:
|
|
return False
|
|
elif s == 1:
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
from sys import argv
|
|
if len(argv) > 1:
|
|
n = int(argv[1])
|
|
else:
|
|
n = 4
|
|
print("%d is a %s number" % (n,"happy" if happynumbers(n) else "unhappy"))
|