<gh_stars>0
def contains(history, num):
    if num in history:
        return True

def next_num(num):
    temp = 0
    while(num != 0):
        d = num % 10
        num = num // 10
        temp += d**2

    return temp

def next_n(num):
    numlist = str(num)
    temp = 0
    for n in numlist:
        n = int(n)
        temp += n**2
    return temp

'''
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits,
and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
'''
class Solution:
    def happyNumber(self, num):
        # numlist = str(num)
        # count = 0

        # while(count < 1000): # count要取多少次不知道 要檢查有沒有cycle detection
        #     temp = 0
        #     for n in numlist:
        #         n = int(n)
        #         temp += n**2

        #     print(temp)  
        #     if temp == 1:
        #         return True
        #     numlist = str(temp)
        #     count += 1

        # return False

        # history = []

        # while(not contains(history, num)):
        #     history.append(num)
        #     num = next_num(num)

        # return num == 1

        # 龜兔賽跑 有撞在一起就代表循環了
        slow = num
        fast = num

        while True:
            slow = next_n(slow)
            # print(slow)
            fast = next_n(next_n(fast))
            # print(fast)
            if fast == slow: break

        return fast == 1 # or slow == 1



if __name__ == '__main__':
    num = 31
    print(Solution().happyNumber(num))
def contains(history, num):
    if num in history:
        return True

def next_num(num):
    temp = 0
    while(num != 0):
        d = num % 10
        num = num // 10
        temp += d**2

    return temp

def next_n(num):
    numlist = str(num)
    temp = 0
    for n in numlist:
        n = int(n)
        temp += n**2
    return temp

'''
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits,
and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
'''
class Solution:
    def happyNumber(self, num):
        # numlist = str(num)
        # count = 0

        # while(count < 1000): # count要取多少次不知道 要檢查有沒有cycle detection
        #     temp = 0
        #     for n in numlist:
        #         n = int(n)
        #         temp += n**2

        #     print(temp)  
        #     if temp == 1:
        #         return True
        #     numlist = str(temp)
        #     count += 1

        # return False

        # history = []

        # while(not contains(history, num)):
        #     history.append(num)
        #     num = next_num(num)

        # return num == 1

        # 龜兔賽跑 有撞在一起就代表循環了
        slow = num
        fast = num

        while True:
            slow = next_n(slow)
            # print(slow)
            fast = next_n(next_n(fast))
            # print(fast)
            if fast == slow: break

        return fast == 1 # or slow == 1



if __name__ == '__main__':
    num = 31
    print(Solution().happyNumber(num))
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.  Example: 19 is a happy number 1^2 + 9^2 = 82 8^2 + 2^2 = 68 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 # numlist = str(num) # count = 0 # while(count < 1000): # count要取多少次不知道 要檢查有沒有cycle detection #     temp = 0 #     for n in numlist: #         n = int(n) #         temp += n**2 #     print(temp) #     if temp == 1: #         return True #     numlist = str(temp) #     count += 1 # return False # history = [] # while(not contains(history, num)): #     history.append(num) #     num = next_num(num) # return num == 1 # 龜兔賽跑 有撞在一起就代表循環了 # print(slow) # print(fast) # or slow == 1