Given two integers A and B, the task is to convert A to B with minimum number of following operations:
- Multiply A by any prime number.
- Divide A by one of its prime divisors.
Print the minimum number of operations required.
Input: A = 10, B = 15
Operation 1: 10 / 2 = 5
Operation 2: 5 * 3 = 15
Input: A = 9, B = 7
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:
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Convert a number m to n using minimum number of given operations
- Number of steps to convert to prime factors
- Minimum number of operations required to reduce N to 1
- Minimum number of operations on an array to make all elements 0
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Find minimum number of merge operations to make an array palindrome
- Minimum absolute difference of a number and its closest prime
- Minimum number of operations to move all uppercase characters before all lower case characters
- Insert minimum number in array so that sum of array becomes prime
- Print the nearest prime number formed by adding prime numbers to N
- Minimum operations to make GCD of array a multiple of k
- Minimum gcd operations to make all array elements one
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral