# All possible co-prime distinct element pairs within a range [L, R]?

CServer Side ProgrammingProgramming

Here we will see how to count number of co-prime pairs from the range, where a number will not appear more than a single pair.

Before discussing the logic, let us see what are the co-prime numbers? The co-prime numbers are those numbers which has only one positive integer divisor, that is 1. In other words, we can say the GCD of these two numbers is 1.

Here we are providing the lower and upper limit. If the lower and upper limits are 1 and 6, then there are three pairs. These are (1, 2), (3, 4) and (5, 6)

The approach for solving this problem is like that: If the numbers are consecutive, they are always co-prime. So the count will be (R – L + 1)/2. If (R – L + 1) is odd then there will be one number left, that will not be places into any pair, if this is even, then all will make pair

## Algorithm

#### countCoPrimePairs(L, R)

Begin
return (R – L + 1)/2
End

## Example

#include <iostream>
using namespace std;
int countCoPrimePairs(int L, int R) {
return (R - L + 1)/2;
}
main() {
int l = 1, r = 6;
cout << "Number of co-prime pairs: " << countCoPrimePairs(l, r);
}

## Output

Number of co-prime pairs: 3
Published on 31-Jul-2019 13:08:45