# Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++

We are given an input N. The goal is to find all pairs of A, B such that 1<=A<=N and 1<=B<=N and GCD(A, B) is B. All pairs have the greatest common divisor as B.

Let us understand with examples.

Input − N=5

Output − Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B are − 10

Explanation

pairs (A <= N, B <= N) such that gcd (A , B) is B are −
(1,1), (2,1),(3,1),(4,1),(5,1),(2,2),(3,3),(4,2),(4,4), (5,5). Total 10.

Input − N=50

Output − Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B are − 207

Explanation

pairs (A <= N, B <= N) such that gcd (A , B) is B are :
(1,1), (2,1),(3,1),(4,1),(5,1).....(50,1)
(2,2),(3,3),(4,4).....(50,50)

Similarly other pairs like (4,2), (6,3), (8,2),(8,4),...........(50,25). Total 207

## The approach used in the below program is as follows

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach.

• Take an integer N as input.

• Function GCD(int A, int B) takes two integers and returns the greatest common divisor of A and B. It calculates gcd recursively.

• If any of the A or B is 0 return another one. If both are equal return any of two values. If A>B return (A-B,B). If B is greater, return gcd(B-A,A). In the end we get gcd value.

• Function count_pairs(int N) takes N and returns the number of pairs such that in pair(A,B), B is gcd and both are in range[1,N].

• Take the initial value as count =0 for the number of such pairs.

• For each value of pair, run for loop i=1 to i=N for A and nested for loop j=1 t j=N for B.

• Make a pair (i,j) and pass to GCD(i,j). If the result is equal to j. Increment count.

• At the end of both loops return count as result.

## Efficient approach

As we can see the gcd(a,b)=b means a is always a multiple of b. All such multiples of b(1<=b<=N) that are less than N will make a pair. For a number i if multiples of i are less than floor(N/i) will be counted.

• Function count_pairs(int N) takes N and returns the number of pairs such that in pair(A,B), B is gcd and both are in range[1,N].

• Take the initial value as count =0 for the number of such pairs.

• Take temporary variable temp=N and i=1.

• Using while (i<=N) do following

• For each i calculate limit of multiples as j=N/temp

• Number of pairs for current i will be temp*(i-j+1). Add to count.

• Set i=j+1. For next B of (A,B).

• Set temp=N/i for next iteration.

• At the end of the while loop, return count as a result.

## Example(naive approach)

Live Demo

#include <iostream>
using namespace std;
int GCD(int A, int B){
if (A == 0){
return B;
}
if (B == 0){
return A;
}
if (A == B){
return A;
}
if (A > B){
return GCD(A-B, B);
}
return GCD(A, B-A);
}
int count_pairs(int N){
int count = 0;
for(int i=1; i<=N; i++){
for(int j = 1; j<=N; j++){
if(GCD(i, j)==j){
count++;
}
}
}
return count;
}
int main(){
int N = 4;
cout<<"Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B are: "<<count_pairs(N);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B are: 8

## Example (Efficient approach)

Live Demo

#include <bits/stdc++.h>
using namespace std;
int Count_pairs(int N){
int count = 0;
int temp = N;
int i = 1;
while(i <= N){
int j = N / temp;
count += temp * (j - i + 1);
i = j + 1;
temp = N / i;
}
return count;
}
int main(){
int N = 4;
cout<<"Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B are: "<<Count_pairs(N);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B are: 8

Updated on: 01-Dec-2020

458 Views
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}