Minimum steps to reach N from 1 by multiplying each step by 2 , 3 , 4 or 5 | Function to find a minimum number of steps to reach N from 1 ; Check until N is greater than 1 and operations can be applied ; Condition to choose the operations greedily ; Driver code
def Minsteps ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( n > 1 ) : NEW_LINE INDENT if ( n % 5 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 5 NEW_LINE continue NEW_LINE DEDENT elif ( n % 4 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 4 NEW_LINE continue NEW_LINE DEDENT elif ( n % 3 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 3 NEW_LINE continue NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 2 NEW_LINE continue NEW_LINE DEDENT return - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 10 NEW_LINE print ( Minsteps ( n ) ) NEW_LINE