# Minimum prime number operations to convert A to B

Given two integers A and B, the task is to convert A to B with minimum number of following operations:

1. Multiply A by any prime number.
2. Divide A by one of its prime divisors.

Print the minimum number of operations required.

Examples:

Input: A = 10, B = 15
Output: 2
Operation 1: 10 / 2 = 5
Operation 2: 5 * 3 = 15

Input: A = 9, B = 7
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: If prime factorization of A = p1q1 * p2q2 * … * pnqn. If we multiply A by some prime then qi for that prime will increase by 1 and if we divide A by one of its prime factors then qi for that prime will decrease by 1. So for a prime p if it occurs qA times in prime factorization of A and qB times in prime factorization of B then we only need to find the sum of |qA – qB| for all the primes to get minimum number of operations.

Efficient Approach: Eliminate all the common factors of A and B by dividing both A and B by their GCD. If A and B have no common factors then we only need the sum of powers of their prime factors to convert A to B.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of ` `// prime factors of a number ` `int` `countFactors(``int` `n) ` `{ ` `    ``int` `factors = 0; ` ` `  `    ``for` `(``int` `i = 2; i * i <= n; i++) { ` `        ``while` `(n % i == 0) { ` `            ``n /= i; ` `            ``factors += 1; ` `        ``} ` `    ``} ` ` `  `    ``if` `(n != 1) ` `        ``factors++; ` ` `  `    ``return` `factors; ` `} ` ` `  `// Function to return the minimum number of ` `// given operations required to convert A to B ` `int` `minOperations(``int` `A, ``int` `B) ` `{ ` `    ``int` `g = __gcd(A, B); ``// gcd(A, B); ` ` `  `    ``// Eliminate the common ` `    ``// factors of A and B ` `    ``A /= g; ` `    ``B /= g; ` ` `  `    ``// Sum of prime factors ` `    ``return` `countFactors(A) + countFactors(B); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A = 10, B = 15; ` ` `  `    ``cout << minOperations(A, B); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `import` `java .io.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the count of ` `// prime factors of a number ` `static` `int` `countFactors(``int` `n) ` `{ ` `    ``int` `factors = ``0``; ` ` `  `    ``for` `(``int` `i = ``2``; i * i <= n; i++)  ` `    ``{ ` `        ``while` `(n % i == ``0``)  ` `        ``{ ` `            ``n /= i; ` `            ``factors += ``1``; ` `        ``} ` `    ``} ` ` `  `    ``if` `(n != ``1``) ` `        ``factors++; ` ` `  `        ``return` `factors; ` `} ` ` `  `static` `int` `__gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == ``0``) ` `    ``return` `a; ` `    ``return` `__gcd(b, a % b); ` `} ` ` `  `// Function to return the minimum  ` `// number of given operations ` `// required to convert A to B ` `static` `int` `minOperations(``int` `A, ``int` `B) ` `{ ` `    ``int` `g = __gcd(A, B); ``// gcd(A, B); ` ` `  `    ``// Eliminate the common ` `    ``// factors of A and B ` `    ``A /= g; ` `    ``B /= g; ` ` `  `    ``// Sum of prime factors ` `    ``return` `countFactors(A) + countFactors(B); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `A = ``10``, B = ``15``; ` ` `  `    ``System.out.println(minOperations(A, B)); ` `} ` `} ` ` `  `// This code is contributed ` `// by Code_Mech `

## Python3

 `# Python3 implementation of above approach  ` ` `  `# from math lib import sqrt  ` `# and gcd function ` `from` `math ``import` `sqrt, gcd ` ` `  `# Function to return the count of  ` `# prime factors of a number  ` `def` `countFactors(n) :  ` `    ``factors ``=` `0``;  ` ` `  `    ``for` `i ``in` `range``(``2``, ``int``(sqrt(n)) ``+` `1``) : ` `        ``while` `(n ``%` `i ``=``=` `0``) :  ` `            ``n ``/``/``=` `i  ` `            ``factors ``+``=` `1` ` `  `    ``if` `(n !``=` `1``) : ` `        ``factors ``+``=` `1` ` `  `    ``return` `factors  ` ` `  `# Function to return the minimum number of  ` `# given operations required to convert A to B  ` `def` `minOperations(A, B) : ` `     `  `    ``g ``=` `gcd(A, B) ` ` `  `    ``# Eliminate the common  ` `    ``# factors of A and B  ` `    ``A ``/``/``=` `g ` `    ``B ``/``/``=` `g ` ` `  `    ``# Sum of prime factors  ` `    ``return` `countFactors(A) ``+` `countFactors(B)  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``A, B ``=` `10``, ``15` ` `  `    ``print``(minOperations(A, B)) ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of above approach ` `using` `System; ` `     `  `class` `GFG ` `{ ` `     `  `    ``// Function to return the count of ` `    ``// prime factors of a number ` `    ``static` `int` `countFactors(``int` `n) ` `    ``{ ` `        ``int` `factors = 0; ` `        ``for` `(``int` `i = 2; i * i <= n; i++)  ` `        ``{ ` `            ``while` `(n % i == 0)  ` `            ``{ ` `                ``n /= i; ` `                ``factors += 1; ` `            ``} ` `        ``} ` ` `  `        ``if` `(n != 1) ` `            ``factors++; ` ` `  `        ``return` `factors; ` `    ``} ` ` `  `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `    ``{ ` `        ``if` `(b == 0) ` `            ``return` `a; ` `        ``return` `__gcd(b, a % b); ` `    ``} ` ` `  `    ``// Function to return the minimum  ` `    ``// number of given operations ` `    ``// required to convert A to B ` `    ``static` `int` `minOperations(``int` `A, ``int` `B) ` `    ``{ ` `        ``int` `g = __gcd(A, B); ``// gcd(A, B); ` ` `  `        ``// Eliminate the common ` `        ``// factors of A and B ` `        ``A /= g; ` `        ``B /= g; ` ` `  `        ``// Sum of prime factors ` `        ``return` `countFactors(A) + countFactors(B); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `A = 10, B = 15; ` `        ``Console.WriteLine(minOperations(A, B)); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// PrinciRaj1992  `

## PHP

 ` ``\$b``) ` `        ``return` `__gcd(``\$a` `- ``\$b``, ``\$b``); ` `             `  `    ``return` `__gcd(``\$a``, ``\$b` `- ``\$a``); ` `} ` ` `  `// Function to return the count of ` `// prime factors of a number ` `function` `countFactors(``\$n``) ` `{ ` `    ``\$factors` `= 0; ` ` `  `    ``for` `(``\$i` `= 2; ``\$i` `* ``\$i` `<= ``\$n``; ``\$i``++) ` `    ``{ ` `        ``while` `(``\$n` `% ``\$i` `== 0)  ` `        ``{ ` `            ``\$n` `/= ``\$i``; ` `            ``\$factors` `+= 1; ` `        ``} ` `    ``} ` ` `  `    ``if` `(``\$n` `!= 1) ` `        ``\$factors``++; ` ` `  `    ``return` `\$factors``; ` `} ` ` `  `// Function to return the minimum number of ` `// given operations required to convert A to B ` `function` `minOperations(``\$A``, ``\$B``) ` `{ ` `    ``\$g` `= __gcd(``\$A``, ``\$B``); ``// gcd(A, B); ` ` `  `    ``// Eliminate the common ` `    ``// factors of A and B ` `    ``\$A` `/= ``\$g``; ` `    ``\$B` `/= ``\$g``; ` ` `  `    ``// Sum of prime factors ` `    ``return` `countFactors(``\$A``) +  ` `           ``countFactors(``\$B``); ` `} ` ` `  `// Driver code ` `\$A` `= 10; ``\$B` `= 15; ` ` `  `echo` `minOperations(``\$A``, ``\$B``); ` ` `  `// This code is contributed  ` `// by Akanksha Rai ` `?> `

Output:

```2
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.