- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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 Articles
- 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++
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- If $n(A)=30, n(B) = 45, n(A cup B) = 65$ then find $n(A cap B)$, $n(A-B)$ and $n(B-A)$.
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1
- Find all pairs (a, b) in an array such that a % b = k in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Write five pairs of integers $(a, b)$ such that $a ÷ b=-3$. One such pair is $(6, -2)$ because $6 ÷ (-2) = (-3)$.
- Write five pairs of integers(a,b) such that a=b=-5 where one such pair (-25,5)because -25+5=-5
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- $triangle A B C$ is an isosceles triangle such that $A B=A C$, $A D perp B C$a) Prove that $triangle A B D cong triangle A C D$b) Prove that $angle B=angle C$c) Is D the mid-point of BC?"\n
- In the adjoining figure, ( Delta A B C ) is an isosceles triangle such that ( A B=A C ). Side BA is produced to D such that $AD=AB$. Show that ( angle B C D=90^{circ} ). "\n
- Add N digits to A such that it is divisible by B after each addition?
- Add N digits to A such that it is divisible by B after each addition in C++?
- In the figure, ( A B C ) is a right triangle right-angled at ( B ) such that ( B C=6 mathrm{~cm} ) and ( A B=8 mathrm{~cm} ) Find the radius of its incircle."\n