Count ordered pairs of numbers with a given LCM
Last Updated :
26 Apr, 2023
Given an integer N, the task is to count the total number of ordered pairs such that the LCM of each pair is equal to N.
Examples:
Input: N = 6
Output: 9
Explanation:
Pairs with LCM equal to N(= 6) are {(1, 6), (2, 6), (2, 3), (3, 6), (6, 6), (6, 3), (3, 2), (6, 2), (6, 1)}
Therefore, the output is 9.
Input: N = 36
Output: 25
Brute Force Approach:
A brute force approach to solve this problem would be to consider all possible pairs of integers (a,b) such that 1 <= a,b <= N, and then check if their LCM is equal to N. If it is, then count it as a valid pair.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CtOrderedPairs( int N)
{
int count = 1;
for ( int i = 1; i <= N; i++) {
for ( int j = 1; j <= N; j++) {
if (i != j && i * j / __gcd(i, j) == N) {
count++;
}
}
}
return count;
}
int main()
{
int N = 6;
cout << CtOrderedPairs(N);
return 0;
}
|
Python3
from math import gcd
def count_ordered_pairs(N):
count = 1
for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
if i ! = j and i * j / / gcd(i, j) = = N:
count + = 1
return count
N = 6
print (count_ordered_pairs(N))
|
C#
using System;
public class Program {
public static int CtOrderedPairs( int N)
{
int count = 1;
for ( int i = 1; i <= N; i++) {
for ( int j = 1; j <= N; j++) {
if (i != j && i * j / GCD(i, j) == N) {
count++;
}
}
}
return count;
}
public static int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static void Main()
{
int N = 6;
Console.WriteLine(CtOrderedPairs(N));
}
}
|
Java
import java.util.*;
public class Main {
static int ctOrderedPairs( int N)
{
int count = 1 ;
for ( int i = 1 ; i <= N; i++) {
for ( int j = 1 ; j <= N; j++) {
if (i != j && i * j / gcd(i, j) == N) {
count++;
}
}
}
return count;
}
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static void main(String[] args)
{
int N = 6 ;
System.out.println(ctOrderedPairs(N));
}
}
|
Javascript
function count_ordered_pairs(N) {
let count = 1;
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N; j++) {
if (i !== j && (i*j) / gcd(i, j) === N) {
count += 1;
}
}
}
return count;
}
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
const N = 6;
console.log(count_ordered_pairs(N));
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Approach: The problem can be solved based on the following observations:
Consider an ordered pair(X, Y).
X = P1a1 * P2a2 * P3a3 *…..* Pnan
Y = P1b1 * P2b2 * P3b3 *…..* Pnbn
Here, P1, P2, ….., Pn are prime factors of X and Y.
LCM(X, Y) = P1max(a1, b1) * P2max(a2, b2) *……….*Pnmax(an, bn)
Therefore, LCM(X, Y) = N = P1m1 * P2m2 * P3m3 *…..* Pnmn
Therefore, total number of ordered pairs (X, Y)
= [{(m1 + 1)2 – m12} * {(m2 + 1)2 – m22} * ……* {(mn + 1)2 – mn2} ]
= (2*m1+1) * (2*m2+1) * (2*m3+1) * ……..* (2*mn+1).
Follow the steps below to solve the problem:
- Initialize a variable, say, countPower, to store the power of all prime factors of N.
- Calculate the power of all prime factors of N.
- Finally, print the count of ordered pairs(X, Y) using the aforementioned formula.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CtOrderedPairs( int N)
{
int res = 1;
for ( int i = 2; i * i <= N; i++) {
int countPower = 0;
while (N % i == 0) {
countPower++;
N /= i;
}
res = res * (2 * countPower
+ 1);
}
if (N > 1) {
res = res * (2 * 1 + 1);
}
return res;
}
int main()
{
int N = 36;
cout << CtOrderedPairs(N);
}
|
Java
class GFG{
static int CtOrderedPairs( int N)
{
int res = 1 ;
for ( int i = 2 ; i * i <= N; i++)
{
int countPower = 0 ;
while (N % i == 0 )
{
countPower++;
N /= i;
}
res = res * ( 2 * countPower + 1 );
}
if (N > 1 )
{
res = res * ( 2 * 1 + 1 );
}
return res;
}
public static void main(String[] args)
{
int N = 36 ;
System.out.println(CtOrderedPairs(N));
}
}
|
Python3
def CtOrderedPairs(N):
res = 1
i = 2
while (i * i < = N):
countPower = 0
while (N % i = = 0 ):
countPower + = 1
N / / = i
res = res * ( 2 * countPower + 1 )
i + = 1
if (N > 1 ):
res = res * ( 2 * 1 + 1 )
return res
N = 36
print (CtOrderedPairs(N))
|
C#
using System;
class GFG{
static int CtOrderedPairs( int N)
{
int res = 1;
for ( int i = 2; i * i <= N; i++)
{
int countPower = 0;
while (N % i == 0)
{
countPower++;
N /= i;
}
res = res * (2 * countPower + 1);
}
if (N > 1)
{
res = res * (2 * 1 + 1);
}
return res;
}
public static void Main()
{
int N = 36;
Console.WriteLine(CtOrderedPairs(N));
}
}
|
Javascript
<script>
function CtOrderedPairs(N)
{
let res = 1;
for (let i = 2; i * i <= N; i++)
{
let countPower = 0;
while (N % i == 0)
{
countPower++;
N /= i;
}
res = res * (2 * countPower + 1);
}
if (N > 1)
{
res = res * (2 * 1 + 1);
}
return res;
}
let N = 36;
document.write(CtOrderedPairs(N));
</script>
|
Time Complexity: O(√N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...