- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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)

#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)

#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

- Related Questions & Answers
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Add N digits to A such that it is divisible by B after each addition?
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Python a += b is not always a = a + b
- Find FIRST & FOLLOW for the following Grammar. S → A a A | B b B A → b B B → ε
- Add N digits to A such that it is divisible by B after each addition in C++?
- Construct a Turing Machine for L = {a^n b^n | n>=1}
- Construct Deterministic PDA for a^n b^n where n>=1
- Count of all possible values of X such that A % X = B in C++