给定两个正整数a和b以及范围[low,high] 。任务是找到在给定范围内的a和b的最大公约数。如果范围内不存在除数,则打印-1。
例子:
Input : a = 9, b = 27, low = 1, high = 5
Output : 3
3 is the highest number that lies in range
[1, 5] and is common divisor of 9 and 27.
Input : a = 9, b = 27, low = 10, high = 11
Output : -1
这个想法是找到a和b的最大公约数GCD(a,b)。现在观察,GCD(a,b)的除数也是a和b的除数。因此,我们将循环i从1迭代到sqrt(GCD(a,b)),并检查i是否除以GCD(a,b)。同样,观察i是否为GCD(a,b)的除数,那么GCD(a,b)/ i也将为除数。因此,对于每次迭代,如果将i除以GCD(a,b),则i和GCD(a,b)/ i的最大值位于范围之内。
以下是此方法的实现:
C++
// CPP Program to find the Greatest Common divisor
// of two number which is in given range
#include
using namespace std;
// Return the greatest common divisor
// of two numbers
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Return the gretest common divisor of a
// and b which lie in the given range.
int maxDivisorRange(int a, int b, int l, int h)
{
int g = gcd(a, b);
int res = -1;
// Loop from 1 to sqrt(GCD(a, b).
for (int i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0)
res = max({res, i, g / i});
return res;
}
// Driven Program
int main()
{
int a = 3, b = 27, l = 1, h = 5;
cout << maxDivisorRange(a, b, l, h) << endl;
return 0;
}
Java
// Java Program to find the Greatest Common
// divisor of two number which is in given
// range
import java.io.*;
class GFG {
// Return the greatest common divisor
// of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Return the gretest common divisor of a
// and b which lie in the given range.
static int maxDivisorRange(int a, int b,
int l, int h)
{
int g = gcd(a, b);
int res = -1;
// Loop from 1 to sqrt(GCD(a, b).
for (int i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0)
res = Math.max(res,
Math.max(i, g / i));
return res;
}
// Driven Program
public static void main (String[] args)
{
int a = 3, b = 27, l = 1, h = 5;
System.out.println(
maxDivisorRange(a, b, l, h));
}
}
// This code is contributed by anuj_67.
Python3
# Python3 Program to find the
# Greatest Common divisor
# of two number which is
# in given range
# Return the greatest common
# divisor of two numbers
def gcd(a, b):
if(b == 0):
return a;
return gcd(b, a % b);
# Return the gretest common
# divisor of a and b which
# lie in the given range.
def maxDivisorRange(a, b, l, h):
g = gcd(a, b);
res = -1;
# Loop from 1 to
# sqrt(GCD(a, b).
i = l;
while(i * i <= g and i <= h):
# if i divides the GCD(a, b),
# then find maximum of three
# numbers res, i and g/i
if(g % i == 0):
res = max(res,max(i, g/i));
i+=1;
return int(res);
# Driver Code
if __name__ == "__main__":
a = 3;
b = 27;
l = 1;
h = 5;
print(maxDivisorRange(a, b, l, h));
# This code is contributed by mits
C#
// C# Program to find the Greatest Common
// divisor of two number which is in given
// range
using System;
class GFG {
// Return the greatest common divisor
// of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Return the gretest common divisor of a
// and b which lie in the given range.
static int maxDivisorRange(int a, int b,
int l, int h)
{
int g = gcd(a, b);
int res = -1;
// Loop from 1 to sqrt(GCD(a, b).
for (int i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0)
res = Math.Max(res,
Math.Max(i, g / i));
return res;
}
// Driven Program
public static void Main ()
{
int a = 3, b = 27, l = 1, h = 5;
Console.WriteLine(
maxDivisorRange(a, b, l, h));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
输出:
3