Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
Last Updated :
12 Feb, 2025
Given a number n, we need to find the number of ordered pairs of a and b such gcd(a, b) is b itself
Examples :
Input : n = 2
Output : 3
The pairs are (1, 1) (2, 2) and (2, 1)
Input : n = 3
Output : 5
(1, 1) (2, 2) (3, 3) (2, 1) and (3, 1)
[Naive Approach] Counting GCD Pairs by Divisor Property
gcd(a, b) = b means b is a factor of a. So the total number of pairs will be equal to sum of divisors for each a = 1 to n.
Please refer find all divisors of a natural number for implementation.
[Expected Approach] Counting GCD Pairs Using Divisor Multiples and Optimized Summation
gcd(a, b) = b means that a is a multiple of b. So the total number of pairs will be sum of number of multiples of each b (where b varies from 1 to n) which are less than or equal to n.
For a number i, a number of multiples of i is less than or equal to floor(n/i). So what we need to do is just sum the floor(n/i) for each i = 1 to n and print it. But more optimizations can be done. floor(n/i) can have atmost 2*sqrt(n) values for i >= sqrt(n). floor(n/i) can vary from 1 to sqrt(n) and similarly for i = 1 to sqrt(n) floor(n/i) can have values from 1 to sqrt(n). So total of 2*sqrt(n) distinct values .
let floor(n/i) = k
k <= n/i < k + 1
n/k+1 < i <= n/k
floor(n/k+1) < i <= floor(n/k)
Thus for given k the largest value of i for
which the floor(n/i) = k is floor(n/k)
and all the set of i for which the
floor(n/i) = k are consecutive
CPP
// C++ implementation of counting pairs
// such that gcd (a, b) = b
#include <bits/stdc++.h>
using namespace std;
// returns number of valid pairs
int CountPairs(int n)
{
// initialize k
int k = n;
// loop till imin <= n
int imin = 1;
// Initialize result
int ans = 0;
while (imin <= n) {
// max i with given k floor(n/k)
int imax = n / k;
// adding k*(number of i with
// floor(n/i) = k to ans
ans += k * (imax - imin + 1);
// set imin = imax + 1 and k = n/imin
imin = imax + 1;
k = n / imin;
}
return ans;
}
// Driver function
int main()
{
cout << CountPairs(1) << endl;
cout << CountPairs(2) << endl;
cout << CountPairs(3) << endl;
return 0;
}
Java
// Java implementation of counting pairs
// such that gcd (a, b) = b
import java.io.*;
public class GFG {
// returns number of valid pairs
static int CountPairs(int n) {
// initialize k
int k = n;
// loop till imin <= n
int imin = 1;
// Initialize result
int ans = 0;
while (imin <= n) {
// max i with given k floor(n/k)
int imax = n / k;
// adding k*(number of i with
// floor(n/i) = k to ans
ans += k * (imax - imin + 1);
// set imin = imax + 1
// and k = n/imin
imin = imax + 1;
k = n / imin;
}
return ans;
}
// Driver code
public static void main(String[] args) {
System.out.println(CountPairs(1));
System.out.println(CountPairs(2));
System.out.println(CountPairs(3));
}
}
// This code is contributed by Anant Agarwal.
Python
# Python implementation of counting
# pairs such that gcd (a, b) = b
# returns number of valid pairs
def CountPairs(n):
# initialize k
k = n
# loop till imin <= n
imin = 1
# Initialize result
ans = 0
while(imin <= n):
# max i with given k floor(n / k)
imax = n / k
# adding k*(number of i with
# floor(n / i) = k to ans
ans += k * (imax - imin + 1)
# set imin = imax + 1 and
# k = n / imin
imin = imax + 1
k = n / imin
return ans
# Driver code
print(CountPairs(1))
print(CountPairs(2))
print(CountPairs(3))
# This code is contributed by Anant Agarwal.
C#
// C# implementation of counting
// pairs such that gcd (a, b) = b
using System;
class GFG {
// returns number of valid pairs
static int CountPairs(int n)
{
// initialize k
int k = n;
// loop till imin <= n
int imin = 1;
// Initialize result
int ans = 0;
while (imin <= n) {
// max i with given
// k floor(n / k)
int imax = n / k;
// adding k * (number of i
// with floor(n / i) = k
// to ans
ans += k * (imax - imin + 1);
// set imin = imax + 1
// and k = n / imin
imin = imax + 1;
k = n / imin;
}
return ans;
}
// Driver code
public static void Main(String []args)
{
Console.WriteLine(CountPairs(1));
Console.WriteLine(CountPairs(2));
Console.WriteLine(CountPairs(3));
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// Javascript implementation of counting pairs
// such that gcd (a, b) = b
// returns number of valid pairs
function CountPairs(n)
{
// initialize k
let k = n;
// loop till imin <= n
let imin = 1;
// Initialize result
let ans = 0;
while (imin <= n) {
// max i with given k floor(n/k)
let imax = Math.floor(n / k);
// adding k*(number of i with
// floor(n/i) = k to ans
ans += k * (imax - imin + 1);
// set imin = imax + 1 and k = n/imin
imin = imax + 1;
k = Math.floor(n / imin);
}
return ans;
}
// Driver function
document.write(CountPairs(1) + "<br>");
document.write(CountPairs(2) + "<br>");
document.write(CountPairs(3) + "<br>");
// This is code is contributed by Mayank Tyagi
</script>
PHP
<?php
// PHP implementation of counting
// pairs such that gcd (a, b) = b
// returns number of valid pairs
function CountPairs($n)
{
// initialize k
$k = $n;
// loop till imin <= n
$imin = 1;
// Initialize result
$ans = 0;
while ($imin <= $n)
{
// max i with given k floor(n/k)
$imax = $n / $k;
// adding k*(number of i with
// floor(n/i) = k to ans
$ans += $k * ($imax - $imin + 1);
// set imin = imax + 1
// and k = n/imin
$imin = $imax + 1;
$k = (int)($n / $imin);
}
return $ans;
}
// Driver Code
echo(CountPairs(1) . "\n");
echo(CountPairs(2) . "\n");
echo(CountPairs(3) . "\n");
// This code is contributed by Ajit.
?>
Time complexity: O(n). This is because the while loop takes O(n) time to complete since it is looping over all elements of the array.
Auxiliary space: O(1), as no extra space is used.
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers Given two positive integers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4
12 min read
Check if two numbers are co-prime or not Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers Given two positive integers a and b. Find the Least Common Multiple (LCM) of a and b.LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 10, b = 5Output : 10Explanation : 10 is the smallest number divisible by both 10 and 5Input : a = 5, b = 11Output : 55Expla
5 min read
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read