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
(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: 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.
Efficient approach: 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
#include <bits/stdc++.h>
using namespace std;
int CountPairs( int n)
{
int k = n;
int imin = 1;
int ans = 0;
while (imin <= n) {
int imax = n / k;
ans += k * (imax - imin + 1);
imin = imax + 1;
k = n / imin;
}
return ans;
}
int main()
{
cout << CountPairs(1) << endl;
cout << CountPairs(2) << endl;
cout << CountPairs(3) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int CountPairs( int n) {
int k = n;
int imin = 1 ;
int ans = 0 ;
while (imin <= n) {
int imax = n / k;
ans += k * (imax - imin + 1 );
imin = imax + 1 ;
k = n / imin;
}
return ans;
}
public static void main(String[] args) {
System.out.println(CountPairs( 1 ));
System.out.println(CountPairs( 2 ));
System.out.println(CountPairs( 3 ));
}
}
|
Python3
def CountPairs(n):
k = n
imin = 1
ans = 0
while (imin < = n):
imax = n / k
ans + = k * (imax - imin + 1 )
imin = imax + 1
k = n / imin
return ans
print (CountPairs( 1 ))
print (CountPairs( 2 ))
print (CountPairs( 3 ))
|
C#
using System;
class GFG {
static int CountPairs( int n)
{
int k = n;
int imin = 1;
int ans = 0;
while (imin <= n) {
int imax = n / k;
ans += k * (imax - imin + 1);
imin = imax + 1;
k = n / imin;
}
return ans;
}
public static void Main(String []args)
{
Console.WriteLine(CountPairs(1));
Console.WriteLine(CountPairs(2));
Console.WriteLine(CountPairs(3));
}
}
|
PHP
<?php
function CountPairs( $n )
{
$k = $n ;
$imin = 1;
$ans = 0;
while ( $imin <= $n )
{
$imax = $n / $k ;
$ans += $k * ( $imax - $imin + 1);
$imin = $imax + 1;
$k = (int)( $n / $imin );
}
return $ans ;
}
echo (CountPairs(1) . "\n" );
echo (CountPairs(2) . "\n" );
echo (CountPairs(3) . "\n" );
?>
|
Javascript
<script>
function CountPairs(n)
{
let k = n;
let imin = 1;
let ans = 0;
while (imin <= n) {
let imax = Math.floor(n / k);
ans += k * (imax - imin + 1);
imin = imax + 1;
k = Math.floor(n / imin);
}
return ans;
}
document.write(CountPairs(1) + "<br>" );
document.write(CountPairs(2) + "<br>" );
document.write(CountPairs(3) + "<br>" );
</script>
|
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Dec, 2022
Like Article
Save Article