class Solution:
def isHappy(self, n: int) -> bool:
for i in range(100):
ans =0
while n>0:
ans += (n%10)*(n%10)
n //=10
n=ans
if n==1:
return True
return False
def isHappy(self, n: int) -> bool:
def get_next(n):
total_sum = 0
while n > 0:
n, digit = divmod(n, 10)
total_sum += digit ** 2
return total_sum
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = get_next(n)
return n == 1
def isHappy( n: int) -> bool:
def fun(n):
res=0
while(n):
tmp=n%10
res+=pow(tmp,2)
n=n//10
return res
nums=[]
nums.append(n)
print(nums)
while(n):
n=fun(n)
print(n)
if n==1:
return True
if n in nums:
return False
nums.append(n)
print(nums)
isHappy(19)
class Solution:
def isHappy(self, n: int) -> bool:
s = str(n)
if len(s) == 1 and int(s[0]) == 4:
return False
sum_num = 0
for i in range(len(s)):
sum_num += int(s[i]) ** 2
if sum_num == 1:
return True
return self.isHappy(sum_num)