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++
#include <bits/stdc++.h>
using namespace std;
void findTheGreatestX( int P, int Q)
{
map< int , int > divisors;
for ( int i = 2; i * i <= Q; i++) {
while (Q % i == 0 and Q > 1) {
Q /= i;
divisors[i]++;
}
}
if (Q > 1)
divisors[Q]++;
int ans = 0;
for ( auto i : divisors) {
int frequency = i.second;
int temp = P;
int cur = 0;
while (temp % i.first == 0) {
temp /= i.first;
cur++;
}
if (cur < frequency) {
ans = P;
break ;
}
temp = P;
for ( int j = cur; j >= frequency; j--) {
temp /= i.first;
}
ans = max(temp, ans);
}
cout << ans;
}
int main()
{
int P = 10, Q = 4;
findTheGreatestX(P, Q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findTheGreatestX( int P, int Q)
{
HashMap<Integer, Integer> divisors = new HashMap<>();
for ( int i = 2 ; i * i <= Q; i++)
{
while (Q % i == 0 && Q > 1 )
{
Q /= i;
if (divisors.containsKey(i))
{
divisors.put(i, divisors.get(i) + 1 );
}
else
{
divisors.put(i, 1 );
}
}
}
if (Q > 1 )
if (divisors.containsKey(Q))
{
divisors.put(Q, divisors.get(Q) + 1 );
}
else
{
divisors.put(Q, 1 );
}
int ans = 0 ;
for (Map.Entry<Integer, Integer> i : divisors.entrySet())
{
int frequency = i.getValue();
int temp = P;
int cur = 0 ;
while (temp % i.getKey() == 0 )
{
temp /= i.getKey();
cur++;
}
if (cur < frequency)
{
ans = P;
break ;
}
temp = P;
for ( int j = cur; j >= frequency; j--)
{
temp /= i.getKey();
}
ans = Math.max(temp, ans);
}
System.out.print(ans);
}
public static void main(String[] args)
{
int P = 10 , Q = 4 ;
findTheGreatestX(P, Q);
}
}
|
Python3
from collections import defaultdict
def findTheGreatestX(P, Q):
divisors = defaultdict( int )
i = 2
while i * i < = Q:
while (Q % i = = 0 and Q > 1 ):
Q / / = i
divisors[i] + = 1
i + = 1
if (Q > 1 ):
divisors[Q] + = 1
ans = 0
for i in divisors:
frequency = divisors[i]
temp = P
cur = 0
while (temp % i = = 0 ):
temp / / = i
cur + = 1
if (cur < frequency):
ans = P
break
temp = P
for j in range (cur, frequency - 1 , - 1 ):
temp / / = i
ans = max (temp, ans)
print (ans)
if __name__ = = "__main__" :
P = 10
Q = 4
findTheGreatestX(P, Q)
|
C#
using System.Collections.Generic;
using System;
using System.Linq;
class GFG{
static void findTheGreatestX( int P, int Q)
{
Dictionary< int ,
int > divisers = new Dictionary< int ,
int >();
for ( int i = 2; i * i <= Q; i++)
{
while (Q % i == 0 && Q > 1)
{
Q /= i;
if (divisers.ContainsKey(i))
divisers[i]++;
else
divisers[i] = 1;
}
}
if (Q > 1)
{
if (divisers.ContainsKey(Q))
divisers[Q]++;
else
divisers[Q] = 1;
}
int ans = 0;
var val = divisers.Keys.ToList();
foreach ( var key in val)
{
int frequency = divisers[key];
int temp = P;
int cur = 0;
while (temp % key == 0)
{
temp /= key;
cur++;
}
if (cur < frequency)
{
ans = P;
break ;
}
temp = P;
for ( int j = cur; j >= frequency; j--)
{
temp /= key;
}
ans = Math.Max(temp, ans);
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int P = 10, Q = 4;
findTheGreatestX(P, Q);
}
}
|
Javascript
<script>
function findTheGreatestX(P, Q)
{
var divisors = new Map();
for ( var i = 2; i * i <= Q; i++)
{
while (Q % i == 0 && Q > 1)
{
Q = parseInt(Q / i);
if (divisors.has(i))
divisors.set(i, divisors.get(i) + 1)
else
divisors.set(i, 1)
}
}
if (Q > 1)
if (divisors.has(Q))
divisors.set(Q, divisors.get(Q) + 1)
else
divisors.set(Q, 1)
var ans = 0;
divisors.forEach((value, key) => {
var frequency = value;
var temp = P;
var cur = 0;
while (temp % key == 0)
{
temp = parseInt(temp / key);
cur++;
}
if (cur < frequency)
{
ans = P;
}
temp = P;
for ( var j = cur; j >= frequency; j--)
{
temp = parseInt(temp / key);
}
ans = Math.max(temp, ans);
});
document.write(ans);
}
var P = 10, Q = 4;
findTheGreatestX(P, Q);
</script>
|
Time Complexity: O(sqrt(Q) + log(P) * log(Q))
Auxiliary Space: O(Q)
Last Updated :
03 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...