Check if digit cube limit of an integer arrives at fixed point or a limit cycle | Define the limit ; Function to get the sum of cube of digits of a number ; Convert to string to get sum of the cubes of its digits ; Return sum ; Function to check if the number arrives at a fixed point or a cycle ; Stores the values obtained ; Insert N to set s ; Get the next number using F ( N ) ; Check if the next number is repeated or not ; Function to check if digit cube limit of an integer arrives at fixed point or in a limit cycle ; N is a non negative integer ; Function Call ; If the value received is greater than limit ; If the value received is an Armstrong number ; Driver Code ; Function Call
LIMIT = 1000000000 NEW_LINE def F ( N : int ) -> int : NEW_LINE INDENT string = str ( N ) NEW_LINE sum = 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT val = int ( ord ( string [ i ] ) - ord ( '0' ) ) NEW_LINE sum += val * val * val NEW_LINE DEDENT return sum NEW_LINE DEDENT def findDestination ( N : int ) -> int : NEW_LINE INDENT s = set ( ) NEW_LINE prev = N NEW_LINE next = 0 NEW_LINE s . add ( N ) NEW_LINE while ( N <= LIMIT ) : NEW_LINE INDENT next = F ( N ) NEW_LINE if next in s : NEW_LINE INDENT return next NEW_LINE DEDENT prev = next NEW_LINE s . add ( prev ) NEW_LINE N = next NEW_LINE DEDENT return next NEW_LINE DEDENT def digitCubeLimit ( N : int ) -> int : NEW_LINE INDENT if ( N < 0 ) : NEW_LINE INDENT print ( " N ▁ cannot ▁ be ▁ negative " ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = findDestination ( N ) NEW_LINE if ( ans > LIMIT ) : NEW_LINE INDENT print ( " Limit ▁ exceeded " ) NEW_LINE DEDENT elif ( ans == F ( ans ) ) : NEW_LINE INDENT print ( " { } ▁ reaches ▁ to ▁ a ▁ fixed ▁ point : ▁ { } " . format ( N , ans ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " { } ▁ reaches ▁ to ▁ a ▁ limit ▁ cycle : ▁ { } " . format ( N , ans ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE digitCubeLimit ( N ) NEW_LINE DEDENT