📌  相关文章
📜  计算GCD等于给定数字的自然数对

📅  最后修改于: 2021-04-29 08:56:11             🧑  作者: Mango

给定三个正整数LRG 。任务是找到GCD(x,y)= G并且x,y位于L和R之间的对(x,y)的计数。

例子:

Input : L = 1, R = 11, G = 5
Output : 3
(5, 5), (5, 10), (10, 5) are three pair having GCD equal to 5 and lie between 1 and 11.
So answer is 3.

Input : L = 1, R = 10, G = 7
Output : 1

一个简单的解决方案是遍历[L,R]中的所有对。对于每一对,找到其GCD。如果GCD等于g,则增加计数。最后返回计数。

一个有效的解决方案基于以下事实:对于GCD等于g的任何正整数对(x,y),x和y应该被g整除。
观察到,在L和R之间最多会有(R – L)/ g个数,它们可以被g整除。
因此,我们发现L和R之间的数字可被g整除。为此,我们从ceil(L / g)* g开始,并在不超过R的情况下每步增加g,对GCD等于1的数字进行计数。
还,

ceil(L/g) * g = floor((L + g - 1) / g) * g.

下面是上述想法的实现:

C++
// C++ program to count pair in range of natural
// number having GCD equal to given number.
#include 
using namespace std;
  
// Return the GCD of two numbers.
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
  
// Return the count of pairs having GCD equal to g.
int countGCD(int L, int R, int g)
{
    // Setting the value of L, R.
    L = (L + g - 1) / g;
    R = R/ g;
  
    // For each possible pair check if GCD is 1.
    int ans = 0;
    for (int i = L; i <= R; i++)
        for (int j = L; j <= R; j++)
            if (gcd(i, j) == 1)
                ans++;
  
    return ans;
}
  
// Driven Program
int main()
{
    int L = 1, R = 11, g = 5;
    cout << countGCD(L, R, g) << endl;
    return 0;
}


Java
// Java program to count pair in 
// range of natural number having 
// GCD equal to given number.
import java.util.*;
  
class GFG {
      
// Return the GCD of two numbers.
static int gcd(int a, int b) 
{
    return b > 0 ? gcd(b, a % b) : a; 
}
  
// Return the count of pairs
// having GCD equal to g.
static int countGCD(int L, int R, int g) {
      
    // Setting the value of L, R.
    L = (L + g - 1) / g;
    R = R / g;
  
    // For each possible pair check if GCD is 1.
    int ans = 0;
    for (int i = L; i <= R; i++)
    for (int j = L; j <= R; j++)
        if (gcd(i, j) == 1)
        ans++;
  
    return ans;
}
  
// Driver code
public static void main(String[] args) {
      
    int L = 1, R = 11, g = 5;
    System.out.println(countGCD(L, R, g));
}
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python program to count
# pair in range of natural
# number having GCD equal
# to given number.
  
# Return the GCD of two numbers.
def gcd(a,b):
  
    return gcd(b, a % b) if b>0 else a
  
   
# Return the count of pairs
# having GCD equal to g.
def countGCD(L,R,g):
  
    # Setting the value of L, R.
    L = (L + g - 1) // g
    R = R// g
   
    # For each possible pair
    # check if GCD is 1.
    ans = 0
    for i in range(L,R+1):
        for j in range(L,R+1):
            if (gcd(i, j) == 1):
                ans=ans +1
   
    return ans
  
# Driver code
  
L = 1
R = 11
g = 5
  
print(countGCD(L, R, g))
  
# This code is contributed
# by Anant Agarwal.


C#
// C# program to count pair in 
// range of natural number having 
// GCD equal to given number.
using System;
  
class GFG {
      
// Return the GCD of two numbers.
static int gcd(int a, int b) 
{
    return b > 0 ? gcd(b, a % b) : a; 
}
  
// Return the count of pairs
// having GCD equal to g.
static int countGCD(int L, int R,
                    int g)
{
      
    // Setting the value of L, R.
    L = (L + g - 1) / g;
    R = R / g;
  
    // For each possible pair 
    // check if GCD is 1.
    int ans = 0;
    for (int i = L; i <= R; i++)
    for (int j = L; j <= R; j++)
        if (gcd(i, j) == 1)
        ans++;
  
    return ans;
}
  
// Driver code
public static void Main() 
{
      
    int L = 1, R = 11, g = 5;
    Console.WriteLine(countGCD(L, R, g));
}
}
  
// This code is contributed by vt_m.


PHP


输出:

3