Skip to content

Commit 0d4dc45

Browse files
authored
Create 202-Happy-Number.py
1 parent b02aceb commit 0d4dc45

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

202-Happy-Number.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isHappy(self, n: int) -> bool:
3+
slow, fast = n, self.sumSquareDigits(n)
4+
5+
while slow != fast:
6+
fast = self.sumSquareDigits(fast)
7+
fast = self.sumSquareDigits(fast)
8+
slow = self.sumSquareDigits(slow)
9+
10+
return True if fast == 1 else False
11+
12+
def sumSquareDigits(self, n):
13+
output = 0
14+
while n:
15+
output += (n % 10) ** 2
16+
n = n // 10
17+
return output

0 commit comments

Comments
 (0)