Minimum prime number operations to convert A to B
Last Updated :
23 Jun, 2022
Given two integers A and B, the task is to convert A to B with a minimum number of the following operations:
- Multiply A by any prime number.
- 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
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 a 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++
#include <bits/stdc++.h>
using namespace std;
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;
}
int minOperations( int A, int B)
{
int g = __gcd(A, B);
A /= g;
B /= g;
return countFactors(A) + countFactors(B);
}
int main()
{
int A = 10, B = 15;
cout << minOperations(A, B);
return 0;
}
|
Java
import java .io.*;
class GFG
{
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);
}
static int minOperations( int A, int B)
{
int g = __gcd(A, B);
A /= g;
B /= g;
return countFactors(A) + countFactors(B);
}
public static void main(String[] args)
{
int A = 10 , B = 15 ;
System.out.println(minOperations(A, B));
}
}
|
Python3
from math import sqrt, gcd
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
def minOperations(A, B) :
g = gcd(A, B)
A / / = g
B / / = g
return countFactors(A) + countFactors(B)
if __name__ = = "__main__" :
A, B = 10 , 15
print (minOperations(A, B))
|
C#
using System;
class GFG
{
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);
}
static int minOperations( int A, int B)
{
int g = __gcd(A, B);
A /= g;
B /= g;
return countFactors(A) + countFactors(B);
}
public static void Main()
{
int A = 10, B = 15;
Console.WriteLine(minOperations(A, B));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
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 minOperations( $A , $B )
{
$g = __gcd( $A , $B );
$A /= $g ;
$B /= $g ;
return countFactors( $A ) +
countFactors( $B );
}
$A = 10; $B = 15;
echo minOperations( $A , $B );
?>
|
Javascript
<script>
function countFactors(n) {
var factors = 0;
for (i = 2; i * i <= n; i++) {
while (n % i == 0) {
n /= i;
factors += 1;
}
}
if (n != 1)
factors++;
return factors;
}
function __gcd(a , b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
function minOperations(A , B) {
var g = __gcd(A, B);
A /= g;
B /= g;
return countFactors(A) + countFactors(B);
}
var A = 10, B = 15;
document.write(minOperations(A, B));
</script>
|
Time complexity : O(sqrtn*logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...