a = int(raw_input("Enter a number to check if it is happy:"))
l = map(int, str(a))
c = 0
while 1:
for i in l:
b = i
c = c + b **2
if c == 1:
print "Happy Number!"
raw_input("Hit enter to end")
break
elif c == a:
print ("Not a Happy number")
raw_input("Hit enter to end")
break
else:
l = map(int, str(c))
This is the code I have written in Python, it works with the numbers 1 and 0, however any other number I have tried leaves it looping infinitely. Could somebody take a look and see what I have done wrong and suggest a solution? Thanks 🙂
For reference: http://en.wikipedia.org/wiki/Happy_number
Try this: