Python Program to Find Minimum Number of Steps to Reach M from N

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

Try yourselsf:

  1. Find The Minimum Number Of Steps To Reach M From N
  2. Find-The-Minimum-Number-Of-Steps-To-Reach-M-From-N
  3. Python Program To Find The Minimum Sum Of Factors Of A Number
  4. Minimum Steps Code In Python
  5. Minimum Steps Program In Python
  6. Minimum Number Of Steps To Reach A Given Number
  7. Minimum Steps Coding Question
  8. Minimum Steps Code
  9. Minimum Steps Program
  10. Min Steps To One

Related Programs: