Count pairs of natural numbers with GCD equal to given number
Last Updated :
12 Sep, 2023
Given three positive integer L, R, G. The task is to find the count of the pair (x,y) having GCD(x,y) = G and x, y lie between L and R.
Examples:
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
A simple solution is to go through all pairs in [L, R]. For every pair, find its GCD. If GCD is equal to g, then increment count. Finally return count.
An efficient solution is based on the fact that, for any positive integer pair (x, y) to have GCD equal to g, x and y should be divisible by g.
Observe, there will be at most (R – L)/g numbers between L and R which are divisible by g.
So we find numbers between L and R which are divisible by g. For this, we start from ceil(L/g) * g and with increment by g at each step while it doesn’t exceed R, count numbers having GCD equal to 1.
Also,
ceil(L/g) * g = floor((L + g - 1) / g) * g.
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int countGCD( int L, int R, int g)
{
L = (L + g - 1) / g;
R = R/ g;
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;
}
int main()
{
int L = 1, R = 11, g = 5;
cout << countGCD(L, R, g) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int gcd( int a, int b)
{
return b > 0 ? gcd(b, a % b) : a;
}
static int countGCD( int L, int R, int g) {
L = (L + g - 1 ) / g;
R = R / g;
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;
}
public static void main(String[] args) {
int L = 1 , R = 11 , g = 5 ;
System.out.println(countGCD(L, R, g));
}
}
|
Python3
def gcd(a,b):
return gcd(b, a % b) if b> 0 else a
def countGCD(L,R,g):
L = (L + g - 1 ) / / g
R = R / / g
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
L = 1
R = 11
g = 5
print (countGCD(L, R, g))
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
return b > 0 ? gcd(b, a % b) : a;
}
static int countGCD( int L, int R,
int g)
{
L = (L + g - 1) / g;
R = R / g;
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;
}
public static void Main()
{
int L = 1, R = 11, g = 5;
Console.WriteLine(countGCD(L, R, g));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
return $b ? gcd( $b , $a % $b ) : $a ;
}
function countGCD( $L , $R , $g )
{
$L = ( $L + $g - 1) / $g ;
$R = $R / $g ;
$ans = 0;
for ( $i = $L ; $i <= $R ; $i ++)
for ( $j = $L ; $j <= $R ; $j ++)
if (gcd( $i , $j ) == 1)
$ans ++;
return $ans ;
}
$L = 1;
$R = 11;
$g = 5;
echo countGCD( $L , $R , $g );
?>
|
Javascript
<script>
function gcd(a, b)
{
return b > 0 ? gcd(b, a % b) : a;
}
function countGCD(L, R, g)
{
L = parseInt((L + g - 1) / g, 10);
R = parseInt(R / g, 10);
let ans = 0;
for (let i = L; i <= R; i++)
for (let j = L; j <= R; j++)
if (gcd(i, j) == 1)
ans++;
return ans;
}
let L = 1, R = 11, g = 5;
document.write(countGCD(L, R, g));
</script>
|
Output:
3
Time Complexity : O((r-l)*(r-l)*log(min(k))) where l and r are lower limit, upper limit and k is the number between l and r.
Space Complexity : O(logk)
Share your thoughts in the comments
Please Login to comment...