# Smallest divisor D of N such that gcd(D, M) is greater than 1

Given two positive integers N and M., The task is to find the smallest divisor D of N such that gcd(D, M) > 1. If there are no such divisors, then print -1.
Examples:

Input: N = 8, M = 10
Output: 2
Input: N = 8, M = 1
Output: -1

A naive approach is to iterate for every factor and calculate the gcd of the factor and M. If it exceeds M, then we have the answer.
Time Complexity: O(N * log max(N, M))
An efficient approach is to iterate till sqrt(n) and check for gcd(i, m). If gcd(i, m) > 1, then we print and break it, else we check for gcd(n/i, m) and store the minimum of them.
Below is the implementation of the above approach.

## C++

 `// C++ implementation of the above approach``#include ``using` `namespace` `std;` `// Function to find the minimum divisor``int` `findMinimum(``int` `n, ``int` `m)``{``    ``int` `mini = m;` `    ``// Iterate for all factors of N``    ``for` `(``int` `i = 1; i * i <= n; i++) {``        ``if` `(n % i == 0) {``            ``int` `sec = n / i;` `            ``// Check for gcd > 1``            ``if` `(__gcd(m, i) > 1) {``                ``return` `i;``            ``}` `            ``// Check for gcd > 1``            ``else` `if` `(__gcd(sec, m) > 1) {``                ``mini = min(sec, mini);``            ``}``        ``}``    ``}` `    ``// If gcd is m itself``    ``if` `(mini == m)``        ``return` `-1;``    ``else``        ``return` `mini;``}``// Drivers code``int` `main()``{``    ``int` `n = 8, m = 10;``    ``cout << findMinimum(n, m);``    ``return` `0;``}`

## Java

 `// Java implementation of the above approach``class` `GFG``{` `static` `int` `__gcd(``int` `a, ``int` `b) ``{ ``    ``if` `(b == ``0``) ``        ``return` `a; ``    ``return` `__gcd(b, a % b); ``    ` `} ` `// Function to find the minimum divisor``static` `int` `findMinimum(``int` `n, ``int` `m)``{``    ``int` `mini = m;` `    ``// Iterate for all factors of N``    ``for` `(``int` `i = ``1``; i * i <= n; i++)``    ``{``        ``if` `(n % i == ``0``)``        ``{``            ``int` `sec = n / i;` `            ``// Check for gcd > 1``            ``if` `(__gcd(m, i) > ``1``) ``            ``{``                ``return` `i;``            ``}` `            ``// Check for gcd > 1``            ``else` `if` `(__gcd(sec, m) > ``1``)``            ``{``                ``mini = Math.min(sec, mini);``            ``}``        ``}``    ``}` `    ``// If gcd is m itself``    ``if` `(mini == m)``        ``return` `-``1``;``    ``else``        ``return` `mini;``}` `// Driver code``public` `static` `void` `main (String[] args) ``{``    ``int` `n = ``8``, m = ``10``;``    ``System.out.println(findMinimum(n, m));``}``}` `// This code is contributed by chandan_jnu`

## Python3

 `# Python3 implementation of the above approach``import` `math` `# Function to find the minimum divisor ``def` `findMinimum(n, m): ` `    ``mini, i ``=` `m, ``1``    ` `    ``# Iterate for all factors of N ``    ``while` `i ``*` `i <``=` `n: ``        ``if` `n ``%` `i ``=``=` `0``: ``            ``sec ``=` `n ``/``/` `i ` `            ``# Check for gcd > 1 ``            ``if` `math.gcd(m, i) > ``1``: ``                ``return` `i ` `            ``# Check for gcd > 1 ``            ``elif` `math.gcd(sec, m) > ``1``: ``                ``mini ``=` `min``(sec, mini) ``            ` `        ``i ``+``=` `1` `    ``# If gcd is m itself ``    ``if` `mini ``=``=` `m:``        ``return` `-``1``    ``else``:``        ``return` `mini ` `# Drivers code ``if` `__name__ ``=``=` `"__main__"``: ` `    ``n, m ``=` `8``, ``10``    ``print``(findMinimum(n, m)) ` `# This code is contributed by Rituraj Jain`

## C#

 `// C# implementation of the above approach``using` `System;` `class` `GFG``{` `static` `int` `__gcd(``int` `a, ``int` `b) ``{ ``    ``if` `(b == 0) ``        ``return` `a; ``    ``return` `__gcd(b, a % b); ``    ` `} ` `// Function to find the minimum divisor``static` `int` `findMinimum(``int` `n, ``int` `m)``{``    ``int` `mini = m;` `    ``// Iterate for all factors of N``    ``for` `(``int` `i = 1; i * i <= n; i++)``    ``{``        ``if` `(n % i == 0)``        ``{``            ``int` `sec = n / i;` `            ``// Check for gcd > 1``            ``if` `(__gcd(m, i) > 1) ``            ``{``                ``return` `i;``            ``}` `            ``// Check for gcd > 1``            ``else` `if` `(__gcd(sec, m) > 1)``            ``{``                ``mini = Math.Min(sec, mini);``            ``}``        ``}``    ``}` `    ``// If gcd is m itself``    ``if` `(mini == m)``        ``return` `-1;``    ``else``        ``return` `mini;``}` `// Driver code``static` `void` `Main()``{``    ``int` `n = 8, m = 10;``    ``Console.WriteLine(findMinimum(n, m));``}``}` `// This code is contributed by chandan_jnu`

## PHP

 ` 1``            ``if` `(__gcd(``\$m``, ``\$i``) > 1) ``            ``{``                ``return` `\$i``;``            ``}` `            ``// Check for gcd > 1``            ``else` `if` `(__gcd(``\$sec``, ``\$m``) > 1)``            ``{``                ``\$mini` `= min(``\$sec``, ``\$mini``);``            ``}``        ``}``    ``}` `    ``// If gcd is m itself``    ``if` `(``\$mini` `== ``\$m``)``        ``return` `-1;``    ``else``        ``return` `\$mini``;``}` `// Driver code``\$n` `= 8; ``\$m` `= 10;``echo``(findMinimum(``\$n``, ``\$m``));` `// This code is contributed by Code_Mech.`

## Javascript

 ``

Output:
`2`

Time Complexity: O(sqrt(N) * log max(N, M)), as we are using a loop to traverse sqrt(N) times and we are using the inbuilt GCD function in each traversal which costs logN time.  Where N and M are the two integers provided.
Auxiliary Space: O(1), as we are not using any extra space.

Previous
Next