Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number


Introduction

The problem of finding the minimum number of moves to make two given numbers M and N rise by repeatedly including any divisor of a number (except 1 and the number itself) can be approached without utilizing Dynamic Programming. In this issue, we are required to plan methodologies that minimize the number of moves required to attain the specified uniformity. Two approaches are displayed to handle this issue: Greedy Algorithm, Prime Factorization. These approaches utilize distinctive strategies to recognize common divisors and optimize the method of making the numbers rise to investigate these non-Dynamic Programming approaches, we will pick up experiences into alternative strategies for solving this issue effectively.

Approach 1: Greedy Algorithm

Algorithm

  • Step 1 −  Creation of a function gcd(). Initialize a variable movesCount to 0.

  • Step 2 − Discover the most common divisor (GCD) of M and N utilizing the Euclidean algorithm.

  • Step 3 − If the GCD is more prominent than 1, it implies there exists a common divisor other than 1.

  • Step 4 − Increase movesCount by the GCD value. Separate both M and N by the GCD.

  • Step 5 − In case M is still not equal to N, increase movesCount by 2.

  • Step 6 − This step is required to account for the extra moves required to reach the same number.

  • Step 7 − Return movesCount as the least number of moves required to create M and N equal.

Example

#include <stdio.h>

int gcd(int a, int b) {
   if (b == 0)
      return a;
   return gcd(b, a % b);
}

int minMoves(int M, int N) {
   int movesCount = 0;
   int commonDivisor = gcd(M, N);

   if (commonDivisor > 1) {
      movesCount += commonDivisor;
      M /= commonDivisor;
      N /= commonDivisor;
   }

   if (M != N)
      movesCount += 2;

   return movesCount - 3;
}
//define main function
int main() {
   int M = 10;
   int N = 30;

   int result = minMoves(M, N);
   printf("Minimum number of moves required: %d\n", result);

   return 0;
}

Output

Minimum number of moves required: 9

Approach 2: Prime Factorization

Algorithm

  • Step 1 −  Define the user-defined function minMoves().

  • Step 2 − Discover the prime factorization of both M and N.

  • Step 3 − Repeat through the prime components of M and check if they are shown within the prime variables of N.

  • Step 4 − If a common prime calculation is found, separate both M and N by that prime figure.

  • Step 5 − Increase movesCount by the value of that prime calculate.

  • Step 6 − Use the if statement to check whether M is equal to N or not, and increase movesCount by 2.

    This step is required to account for the extra moves required to reach the same number.

  • Step 7 − Return movesCount as the least number of moves required to form M and N equal.

Example

#include <stdio.h>

int minMoves(int M, int N) {
   int movesCount = 0;

   for (int i = 2; i <= M; i++) {
      while (M % i == 0 && N % i == 0){
         M /= i;
         N /= i;
         movesCount += i;
      }
   }

   if (M != N)
      movesCount += 2;

   return movesCount;
}
int main() {
   int M = 10;
   int N = 30;

   int result = minMoves(M, N);
   printf("Minimum number of moves required: %d\n", result);

   return 0;
}

Output

Minimum number of moves required: 9

Conclusion

The Greedy Algorithm approach utilizes the most noteworthy common divisor (GCD) to distinguish common divisors and minimize the moves required. The Prime Factorization approach variables both numbers into primes and checks for common components. The Brute Force efficiently searches for divisors inside a particular run. Whereas these approaches don't utilize Dynamic Programming, they give successful methodologies to unravel the issue and discover the least number of moves required. By applying these methods, we will optimize the method of achieving equality between two numbers utilizing as it were particular divisors.

Updated on: 25-Aug-2023

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements