Count of Prime Factors of N to be Added at each Step to Convert N to M


In this problem, we will convert the number N to M by adding the one prime factor of N to itself and updating it in each operation.

We will use the breadth−first search algorithm to solve the problem. We will find the prime factor of each updated N and insert it into the queue after adding it to the prime factor of N. Also, we will define functions to find the smallest prime factor of a particular number.

Problem statement − We have given N and M integer values. We need to count the minimum number of operations required to convert N to M. In each operation, we need to take the one prime factor of the updated value of N and add it to the N. If it is possible the convert N to M, print the minimum operation count. Otherwise, print −1.

Sample examples

Input

N = 8, M = 12;

Output

2

Explanation 

  • In the first operation, take ‘2’ (prime factor of 8) and add it to 8. So, N becomes 10.

  • In the next operation, again add 2 to 10.

Input

N = 7, M = 13;

Output

-1

Explanation − 7 and 13 are both prime numbers. So, it is not possible to convert N to M.

Input

N = 7, M = 14;

Output

1

Explanation − The 7 is the prime factor of 7. So, when we add 7 to 7, we can get 14 in one operation.

Approach

In this approach, we will use the sieve algorithm to calculate the smallest prime factor of each number in the range of 2 to 100009.

After that, we will use the queue data structure to maintain the updated N value after adding the prime factor and map it with the number of operations required to reach the updated value.

For example, we want to reach 3 to 9.

  • In the first step, we will add 3 to the queue with 0 distance.

  • After that, we will get the prime factors of 3, which is {3}.

  • In the next step, we add 6 (3 + 3) to the queue with distance 1.

  • After that, we will find the prime factors of 6, which is {2, 3}.

  • So, we will insert the {8, 2} (6+2, 1+1) and {9, 2} (6 + 3, 1 + 1) pairs into the queue. Here, the first element of the pair is the updated value, and the second element of the pair is the distance after incrementing by 1.

  • After that, we get the {8, 2} pair from the queue. The prime factor of 8 is {2}. So, we insert the {10, 3} into the queue.

  • Next, we get the {9, 2} from the queue. Here, 9 is equal to M. So, we print 2 as an answer.

Algorithm

Step 1 − Define the prime[] array of size 100009 to store the smallest prime factors of each number.

Step 2 − After that, define the getPrimeNumbers() function to find the smallest prime factor of each number.

Step 2.1 − Initialize all the prime[] array elements with −1.

Step 2.2 − Start traversing the array from 2 and traverse until p*p is less than 100009. Also, use a nested loop to update every pth index starting from the pth index.

Step 2.3 − In the nested loop, if prime[q] is −1, update it with the p-value.

Step 3 − Define the getPrimeFactors() function to find all prime factors of the particular value.

Step 3.1 − Define the ‘p_factors’ set to store the prime factors.

Step 3.2 − Traverse the given number until its value becomes less than 1.

Step 3.3 − In the loop, insert the prime[num] into the p_factors set, which is the smallest prime factor of the number.

Step 3.4 − Divide the number with the smallest prime factor.

Step 3.5 − When the loop completes all iterations, return the p_factors set.

Step 4 − In the findMinimumSteps() function, define the queue to store the pair of updated N values and a number of required operations. Also, insert the {n, 0} pair into the queue.

Step 5 − Make iterations until the queue becomes empty.

Step 5.1 − Pop up the first pair from the queue. Also, store the first element of the pair in the ‘temp’ variable and the second element of the pair in the ‘dist’ variable.

Step 5.2 − If the temp is equal to m, return the ‘dist’ value.

Step 5.3 − Break the loop if ‘temp’ is greater than m.

Step 5.4 − Execute the getPrimeFactors() function to get the prime factors of ‘temp’ and store them into the ‘p_factors’ set.

Step 5.5 − Now, traverse each element of the p_factors set.

Step 5.5.1 − Insert {temp + p, dist + 1} pairs into the queue for each prime factor.

Step 6 − Return −1 at the end of the function.

Example

#include <bits/stdc++.h>
using namespace std;
// To store all prime numbers
int prime[100009];
// To find prime numbers
void getPrimeNumbers() {
    // Initializing with -1
    memset(prime, -1, 100005);
    // Pre-computing smallest prime factor
    for (int p = 2; p * p <= 100005; p++) {
        for (int q = p; q <= 100005; q += p) {
            if (prime[q] == -1) {
                prime[q] = p;
            }
        }
    }
}
// Finding unique prime factors of n
set<int> getPrimeFactors(int num) {
    set<int> p_factors;
    // Store distinct prime factors
    while (num > 1) {
        p_factors.insert(prime[num]);
        num /= prime[num];
    }
    return p_factors;
}
int findMinimumSteps(int n, int m) {
    // To store distance from the start node
    queue<pair<int, int>> que;
    // BFS algorithm
    que.push({n, 0});
    while (!que.empty()) {
        // Get the first node from the queue
        int temp = que.front().first;
        int dist = que.front().second;
        que.pop();
            // When the temp is equal to m, we found steps
            if (temp == m) {
                return dist;
            }
            // When it is not possible to convert N to M
            else if (temp > m) {
                break;
            }
        // Finding prime factors
        set<int> p_factors = getPrimeFactors(temp);
        // Traverse prime factors
        for (auto p : p_factors) {
                // Insert pair to queue
                que.push({temp + p, dist + 1});
        }
    }
    // If we can't convert N to M
    return -1;
}
int main() {
    int N = 8, M = 12;
    getPrimeNumbers();
    int res = findMinimumSteps(N, M);
    if (res == -1) {
        cout << "It is not possible to convert " << N << " to " << M;
    } else {
        cout << "The minimum steps required to convert" << N << " to " << M << " is " << res;
    }
}

Output

The minimum steps required to convert8 to 12 is 2

Time complexity − O(M) to convert N to M.

Space complexity −O(M) to store pairs into the queue.

We learned three different sub−problems using a single problem. The first is finding the smallest prime numbers of each number using the sieve algorithm. The second is finding all prime factors of the given number, and the third is converting N to M by adding any prime factor of N to itself.

Updated on: 02-Aug-2023

27 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements