Minimum decrements or division by a proper divisor required to reduce N to 1 | Function to find the minimum number of steps required to reduce N to 1 ; Stores the number of steps required ; If the value of N is equal to 2 or N is odd ; Decrement N by 1 ; Increment cnt by 1 ; If N is even ; Update N ; Increment cnt by 1 ; Return the number of steps obtained ; Driver Code
def reduceToOne ( N ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( N != 1 ) : NEW_LINE INDENT if ( N == 2 or ( N % 2 == 1 ) ) : NEW_LINE INDENT N = N - 1 NEW_LINE cnt += 1 NEW_LINE DEDENT elif ( N % 2 == 0 ) : NEW_LINE INDENT N = N / ( N / 2 ) NEW_LINE cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 35 NEW_LINE print ( reduceToOne ( N ) ) NEW_LINE DEDENT