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++
#include <bits/stdc++.h>
using namespace std;
int findMinimum( int n, int m)
{
int mini = m;
for ( int i = 1; i * i <= n; i++) {
if (n % i == 0) {
int sec = n / i;
if (__gcd(m, i) > 1) {
return i;
}
else if (__gcd(sec, m) > 1) {
mini = min(sec, mini);
}
}
}
if (mini == m)
return -1;
else
return mini;
}
int main()
{
int n = 8, m = 10;
cout << findMinimum(n, m);
return 0;
}
|
Java
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static int findMinimum( int n, int m)
{
int mini = m;
for ( int i = 1 ; i * i <= n; i++)
{
if (n % i == 0 )
{
int sec = n / i;
if (__gcd(m, i) > 1 )
{
return i;
}
else if (__gcd(sec, m) > 1 )
{
mini = Math.min(sec, mini);
}
}
}
if (mini == m)
return - 1 ;
else
return mini;
}
public static void main (String[] args)
{
int n = 8 , m = 10 ;
System.out.println(findMinimum(n, m));
}
}
|
Python3
import math
def findMinimum(n, m):
mini, i = m, 1
while i * i < = n:
if n % i = = 0 :
sec = n / / i
if math.gcd(m, i) > 1 :
return i
elif math.gcd(sec, m) > 1 :
mini = min (sec, mini)
i + = 1
if mini = = m:
return - 1
else :
return mini
if __name__ = = "__main__" :
n, m = 8 , 10
print (findMinimum(n, m))
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static int findMinimum( int n, int m)
{
int mini = m;
for ( int i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
int sec = n / i;
if (__gcd(m, i) > 1)
{
return i;
}
else if (__gcd(sec, m) > 1)
{
mini = Math.Min(sec, mini);
}
}
}
if (mini == m)
return -1;
else
return mini;
}
static void Main()
{
int n = 8, m = 10;
Console.WriteLine(findMinimum(n, m));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function findMinimum( $n , $m )
{
$mini = $m ;
for ( $i = 1; $i * $i <= $n ; $i ++)
{
if ( $n % $i == 0)
{
$sec = $n / $i ;
if (__gcd( $m , $i ) > 1)
{
return $i ;
}
else if (__gcd( $sec , $m ) > 1)
{
$mini = min( $sec , $mini );
}
}
}
if ( $mini == $m )
return -1;
else
return $mini ;
}
$n = 8; $m = 10;
echo (findMinimum( $n , $m ));
|
Javascript
<script>
function __gcd(a , b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
function findMinimum(n , m) {
var mini = m;
for ( var i = 1; i * i <= n; i++) {
if (n % i == 0) {
var sec = n / i;
if (__gcd(m, i) > 1) {
return i;
}
else if (__gcd(sec, m) > 1) {
mini = Math.min(sec, mini);
}
}
}
if (mini == m)
return -1;
else
return mini;
}
var n = 8, m = 10;
document.write(findMinimum(n, m));
</script>
|
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Jun, 2022
Like Article
Save Article