-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathleetcode202.py
40 lines (34 loc) · 927 Bytes
/
leetcode202.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution(object):
def sumOfSquares(self, num):
sum = 0
while (num > 0):
last = int(num % 10)
sum += pow(last, 2)
num /= int(10)
return sum
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
slow = fast = self.sumOfSquares(n)
print(slow)
slow = self.sumOfSquares(slow)
print(slow)
print(fast)
fast = self.sumOfSquares(fast)
print(fast)
fast = self.sumOfSquares(fast)
print(fast)
while (fast != slow):
slow = self.sumOfSquares(slow)
print(slow)
fast = self.sumOfSquares(fast)
print(fast)
fast = self.sumOfSquares(fast)
print(fast)
if fast == slow:
if fast == 1:
return True
return False
print(Solution().isHappy(20))