• Just for fun :-)

``````def isHappy(self, n):
while n != 42:
prev, n = n, sum(d*d for d in map(int, str(n)))
if prev == n:
return True
return False``````

• To my surprise, this is CORRECT!

• Interesting.
What the purpose of using prev? This one also works

``````class Solution(object):
def isHappy(self, n):
while n != 42:
n = sum(d*d for d in map(int, str(n)))
if n==1:
return True
return False

``````

• @ceclinux Because I want 42 to be the only number in the program :-)

Otherwise I would've also used `sum(int(d)**2 for d in str(n))`.

• @StefanPochmann Thanks for the comics!

I will go with this:

``````class Solution(object):
def isHappy(self, n):
while n != 1:
if n == 4:
return False
n = sum(int(c)**2 for c in str(n))
return True
``````

• @o_sharp Hmm, not sure I get the point. Is it that you're using the smallest powers of 2?

• @StefanPochmann 42->20->4->16->37->58->89->145->42
I chose the shortest number in the cycle.

• @o_sharp Ok, so... nothing special about that solution? Many solutions using 4 have been posted already, including one by me :-)

• @StefanPochmann It's special in a dumb way. It came from your 42 solution when I was trying to make it shorter.

• @o_sharp Ha, ok. The 42 solution was really just for fun, not trying to be short :-)

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.