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
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
@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.
@StefanPochmann It's special in a dumb way. It came from your 42 solution when I was trying to make it shorter.