Largest divisor of a number not divisible by another given number

Given two positive integers P and Q, the task is to largest divisor of P which is not divisible by Q.

Examples:

Input: P = 10, Q = 4
Output: 10
Explanation: 10 is the largest number which divides 10 but not divisible by 4.

Input: P = 12, Q = 6
Output: 4
Explanation: 4 is the largest number which divides 12 but not divisible by 6.

Approach: The simplest approach is to find all the divisors of P and obtain the greatest of these divisors, which is not divisible by Q.
Time Complexity: O(√P)
Auxiliary Space: O(1)

Alternate Approach: Follow the steps below to solve the problem:



  • Store the count of frequencies of prime factors of Q in a HashMap divisors.
  • Initialize a variable, say ans, to store the greatest number X, such that X divides P but is not divisible by Q.
  • Iterate through all divisors of Q and perform the following steps:
    • Store the count of frequencies of the current prime divisor in a variable, say frequency.
    • Store the count of frequencies of the current prime divisor on dividing P in a variable, say cur, and initialize num as the current prime divisor * (frequency – cur + 1).
    • If the value of cur is less than frequency, then update the variable ans to P and break out of the loop.
    • Otherwise, divide P with num and store the result in a variable, say tempAns.
    • After completing the above steps, update the value of ans to the maximum of ans and tempAns.
  • After completing the above steps, print the value of ans as the maximum possible result.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the largest number
// X such that it divides P but is not
// divisible by Q
void findTheGreatestX(int P, int Q)
{
    // Stores the frequency count of
    // of all Prime Factors
    map<int, int> divisiors;
  
    for (int i = 2; i * i <= Q; i++) {
  
        while (Q % i == 0 and Q > 1) {
  
            Q /= i;
  
            // Increment the frequency
            // of the current prime factor
            divisiors[i]++;
        }
    }
  
    // If Q is a prime factor
    if (Q > 1)
        divisiors[Q]++;
  
    // Stores the desired result
    int ans = 0;
  
    // Iterate through all divisors of Q
    for (auto i : divisiors) {
  
        int frequency = i.second;
        int temp = P;
  
        // Stores the frequency count
        // of current prime divisor on
        // dividing P
        int cur = 0;
  
        while (temp % i.first == 0) {
            temp /= i.first;
  
            // Count the frequency of the
            // current prime factor
            cur++;
        }
  
        // If cur is less than frequency
        // then P is the final result
        if (cur < frequency) {
            ans = P;
            break;
        }
  
        temp = P;
  
        // Iterate to get temporary answer
        for (int j = cur; j >= frequency; j--) {
            temp /= i.first;
        }
  
        // Update current answer
        ans = max(temp, ans);
    }
  
    // Print the desired result
    cout << ans;
}
  
// Driver Code
int main()
{
    // Given P and Q
    int P = 10, Q = 4;
  
    // Function Call
    findTheGreatestX(P, Q);
  
    return 0;
}

chevron_right


Output:

10

Time Complexity: O(sqrt(Q) + log(P) * log(Q))
Auxiliary Space: O(Q)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.