Given two numbers m and n the task is to get the lowest number of steps to get from M to N in Python We’ll only use two operations to get from M to N.
- Multiply the given number by 2.
- Subtract 1 from the given number.
Examples:
Example1:
Input:
Given Number M=10 Given Number N=6
Output:
The result is 2
Example2:
Input:
Given Number M=12 Given Number N=19
Output:
The result is 7
Program to Find Minimum Number of Steps to Reach M from N in Python
Below are the ways to find the minimum number of steps to reach M from N in Python.
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Method #1: Using While loop (Static Input)
Approach:
- Give the two numbers m and n as static input and store them in two separate variables.
- Take a variable result and initialize the variable with 0.
- Loop till M is greater than N using while loop.
- Check if M is even or odd using the If statement.
- If it is true then increment the value of M by 1 and result by 1.
- Divide the M by 2 after the end of the If statement.
- Increment the result by 1.
- Print the result +N-M.
- The Exit of the Program.
Below is the implementation:
# Give the two numbers m and n as static input and store them in two separate variables. mNumb = 10 nNumb = 6 # Take a variable result and initialize the variable with 0. resu = 0 # Loop till M is greater than N using while loop. while(mNumb > nNumb): # Check if M is even or odd using the If statement. if (mNumb & 1): # If it is true then increment the value of M by 1 and result by 1. mNumb += 1 resu += 1 # Divide the M by 2 after the end of the If statement. mNumb //= 2 # Increment the result by 1. resu += 1 # Print the result +N-M. resu = resu+nNumb-mNumb print('The result is', resu)
Output:
The result is 2
Method #2: Using While loop (User Input)
Approach:
- Give the two numbers m and n as user input using map(), int(), and split() functions.
- Store them in two separate variables.
- Take a variable result and initialize the variable with 0.
- Loop till M is greater than N using while loop.
- Check if M is even or odd using the If statement.
- If it is true then increment the value of M by 1 and result by 1.
- Divide the M by 2 after the end of the If statement.
- Increment the result by 1.
- Print the result +N-M.
- The Exit of the Program.
Below is the implementation:
# Give the two numbers m and n as user input using map(), int(), and split() functions. # Store them in two separate variables. mNumb, nNumb = map(int, input('Enter some random numbers M and N =').split()) # Take a variable result and initialize the variable with 0. resu = 0 # Loop till M is greater than N using while loop. while(mNumb > nNumb): # Check if M is even or odd using the If statement. if (mNumb & 1): # If it is true then increment the value of M by 1 and result by 1. mNumb += 1 resu += 1 # Divide the M by 2 after the end of the If statement. mNumb //= 2 # Increment the result by 1. resu += 1 # Print the result +N-M. resu = resu+nNumb-mNumb print('The result is', resu)
Output:
Enter some random numbers M and N =12 19 The result is 7
Related Programs:
- python program to find the factorial of a number
- python program to find the factors of a number
- python program to find whether a number is a power of two
- python program to find the sum of the series 1 1 2 1 3 1 n
- python program to find the sum of the series 1 x2 2 x3 3 xn n
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to compute the value of eulers number using the formula e 1 1 1 1 2 1 n