Program to implement Collatz Conjecture | Function to find if n reaches to 1 or not . ; If there is a cycle formed , we can 't reach 1. ; If n is odd then pass n = 3 n + 1 else n = n / 2 ; Wrapper over isToOneRec ( ) ; To store numbers visited using recursive calls . ; Driver Code
def isToOneRec ( n : int , s : set ) -> bool : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT if n in s : NEW_LINE INDENT return False NEW_LINE DEDENT if n % 2 : NEW_LINE INDENT return isToOneRec ( 3 * n + 1 , s ) NEW_LINE DEDENT else : NEW_LINE INDENT return isToOneRec ( n // 2 , s ) NEW_LINE DEDENT DEDENT def isToOne ( n : int ) -> bool : NEW_LINE INDENT s = set ( ) NEW_LINE return isToOneRec ( n , s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE if isToOne ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT