Count of all possible pairs having sum of LCM and GCD equal to N
Given an integer N, the task is to find the count of all possible pairs of integers (A, B) such that GCD (A, B) + LCM (A, B) = N.
Examples:
Input: N = 14
Output: 7
Explanation:
All the possible pairs are {1, 13}, {2, 12}, {4, 6}, {6, 4}, {7, 7}, {12, 2}, {13, 1}
Input: N = 6
Output: 5
Approach:
Follow the steps below to solve the problem:
- Initialize a variable count, to store the count of all the possible pairs.
- Iterate over the range [1, N] to generate all possible pairs (i, j). Calculate the GCD of (i, j) using the __gcd() function and calculate LCM of (i, j).
- Now, check if the sum of LCM (i, j) and GCD (i, j) is equal to N or not. If so, increment count.
- Print the count value after the complete traversal of the range.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lcm( int a, int b)
{
return (a * b) / __gcd(a, b);
}
int countPair( int N)
{
int count = 0;
for ( int i = 1;
i <= N; i++) {
for ( int j = 1;
j <= N; j++) {
if (__gcd(i, j)
+ lcm(i, j)
== N) {
count++;
}
}
}
return count;
}
int main()
{
int N = 14;
cout << countPair(N);
return 0;
}
|
Java
class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static int lcm( int a, int b)
{
return (a * b) / __gcd(a, b);
}
static int countPair( int N)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++)
{
for ( int j = 1 ; j <= N; j++)
{
if (__gcd(i, j) + lcm(i, j) == N)
{
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 14 ;
System.out.print(countPair(N));
}
}
|
Python3
def __gcd(a, b):
if b = = 0 :
return a
else :
return __gcd(b, a % b)
def lcm(a, b):
return (a * b) / / __gcd(a, b)
def countPair(N):
count = 0
for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
if (__gcd(i, j) + lcm(i, j) = = N):
count + = 1
return count
if __name__ = = "__main__" :
N = 14
print (countPair(N))
|
C#
using System;
class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static int lcm( int a, int b)
{
return (a * b) / __gcd(a, b);
}
static int countPair( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++)
{
for ( int j = 1; j <= N; j++)
{
if (__gcd(i, j) + lcm(i, j) == N)
{
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int N = 14;
Console.Write(countPair(N));
}
}
|
Javascript
<script>
function __gcd(a , b)
{
return b == 0 ? a : __gcd(b, a % b);
}
function lcm(a , b) {
return (a * b) / __gcd(a, b);
}
function countPair(N)
{
var count = 0;
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
if (__gcd(i, j) + lcm(i, j) == N) {
count++;
}
}
}
return count;
}
var N = 14;
document.write(countPair(N));
</script>
|
Time Complexity: O(N2*log(N)) (here two nested for loops so O(n2) and in between them there is __gcd(a,b) which will run in log(N) time so effective time complexity will be O(N2*log(N)) )
Auxiliary Space: O(logn)
Last Updated :
18 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...