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++

`// C++ program to count pair in range of natural ` `// number having GCD equal to given number. ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count pair ` `// in range of natural number ` `// having GCD equal to given number. ` ` ` `// Return the GCD of two numbers. ` `function` `gcd( ` `$a` `, ` `$b` `) ` `{ ` ` ` `return` `$b` `? gcd(` `$b` `, ` `$a` `% ` `$b` `) : ` `$a` `; ` `} ` ` ` `// Return the count of pairs ` `// having GCD equal to g. ` `function` `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` `= ` `$L` `; ` `$i` `<= ` `$R` `; ` `$i` `++) ` ` ` `for` `(` `$j` `= ` `$L` `; ` `$j` `<= ` `$R` `; ` `$j` `++) ` ` ` `if` `(gcd(` `$i` `, ` `$j` `) == 1) ` ` ` `$ans` `++; ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$L` `= 1; ` ` ` `$R` `= 11; ` ` ` `$g` `= 5; ` ` ` `echo` `countGCD(` `$L` `, ` `$R` `, ` `$g` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

3

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of pairs with sum N from first N natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Count the numbers < N which have equal number of divisors as K
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Divide first N natural numbers into 3 equal sum subsets
- Count of all possible pairs having sum of LCM and GCD equal to N
- Count pairs with set bits sum equal to K
- Count pairs from two arrays having sum equal to K
- Count natural numbers whose factorials are divisible by x but not y
- Count of natural numbers in range [L, R] which are relatively prime with N
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count all distinct pairs with product equal to K
- Count of pairs from Array with sum equal to twice their bitwise AND
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Find the count of natural Hexadecimal numbers of size N
- Count non decreasing subarrays of size N from N Natural numbers