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!
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?
I chose the shortest number in the cycle.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.