Minimum number to be added to minimize LCM of two given numbers
Last Updated :
23 Jun, 2021
Given two numbers A and B, the task is to find the minimum number that needs to be added to A and B such that their LCM is minimized.
Examples:
Input: A = 6, B = 10
Output: 2
Explanation: On adding 2 to A and B, the value becomes 8 and 12 respectively. LCM of 8 and 12 is 24, which is the minimum LCM possible.
Input: A = 5, B = 10
Output: 0
Explanation:
10 is already the minimum LCM of both the given number.
Hence, the minimum number added is 0.
Approach: The idea is based on the generalized formula that the LCM of (A + x) and (B + x) is equal to (A + x)*(B + x) / GCD(A + x, B + x). It can be observed that GCD of (A + x) and (B + x) is is equal to the GCD of (B – A) and (A + x). So, the gcd is a divisor of (B ? A).
Therefore, iterate over all the divisors of (B ? A) and if A % M = B % M (if M is one of the divisors), then the value of X(the minimum value must be added) is equal to M ? A % M.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
vector< int > getDivisors( int diff)
{
vector< int > divisor;
for ( int i = 1; i * i <= diff; i++) {
if (i == diff / i) {
divisor.push_back(i);
continue ;
}
if (diff % i == 0) {
divisor.push_back(i);
divisor.push_back(diff / i);
}
}
return divisor;
}
int findTheSmallestX( int a, int b)
{
int diff = b - a;
vector< int > divisor
= getDivisors(diff);
int lcm = (a * b) / __gcd(a, b);
int ans = 0;
for ( int i = 0;
i < ( int )divisor.size(); i++) {
int x = (divisor[i]
- (a % divisor[i]));
if (!x)
continue ;
int product = (b + x) * (a + x);
int tempGCD = __gcd(a + x, b + x);
int tempLCM = product / tempGCD;
if (lcm > tempLCM) {
ans = x;
lcm = tempLCM;
}
}
cout << ans;
}
int main()
{
int A = 6, B = 10;
findTheSmallestX(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
static int [] getDivisors( int diff)
{
Vector<Integer> divisor =
new Vector<>() ;
for ( int i = 1 ;
i * i <= diff; i++)
{
if (i == diff / i)
{
divisor.add(i);
continue ;
}
if (diff % i == 0 )
{
divisor.add(i);
divisor.add(diff / i);
}
}
int []ans = new int [divisor.size()];
int j = 0 ;
for ( int i: divisor)
ans[j] = i;
return ans;
}
static void findTheSmallestX( int a,
int b)
{
int diff = b - a;
int [] divisor =
getDivisors(diff);
int lcm = (a * b) /
__gcd(a, b);
int ans = 0 ;
for ( int i = 0 ;
i <divisor.length; i++)
{
int x = 0 ;
if (divisor[i] != 0 )
x = (divisor[i] -
(a % divisor[i]));
if (x == 0 )
continue ;
int product = (b + x) *
(a + x);
int tempGCD = __gcd(a + x,
b + x);
int tempLCM = product /
tempGCD;
if (lcm > tempLCM)
{
ans = x;
lcm = tempLCM;
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int A = 6 , B = 10 ;
findTheSmallestX(A, B);
}
}
|
Python3
from math import gcd
def getDivisors(diff):
divisor = []
for i in range ( 1 , diff):
if i * i > diff:
break
if (i = = diff / / i):
divisor.append(i)
continue
if (diff % i = = 0 ):
divisor.append(i)
divisor.append(diff / / i)
return divisor
def findTheSmallestX(a, b):
diff = b - a
divisor = getDivisors(diff)
lcm = (a * b) / / gcd(a, b)
ans = 0
for i in range ( len (divisor)):
x = (divisor[i] - (a % divisor[i]))
if ( not x):
continue
product = (b + x) * (a + x)
tempGCD = gcd(a + x, b + x)
tempLCM = product / / tempGCD
if (lcm > tempLCM):
ans = x
lcm = tempLCM
print (ans)
if __name__ = = '__main__' :
A = 6
B = 10
findTheSmallestX(A, B)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
static int [] getDivisors( int diff)
{
List< int > divisor = new List< int >();
for ( int i = 1; i * i <= diff; i++)
{
if (i == diff / i)
{
divisor.Add(i);
continue ;
}
if (diff % i == 0)
{
divisor.Add(i);
divisor.Add(diff / i);
}
}
int []ans = new int [divisor.Count];
int j = 0;
foreach ( int i in divisor)
ans[j] = i;
return ans;
}
static void findTheSmallestX( int a,
int b)
{
int diff = b - a;
int [] divisor = getDivisors(diff);
int lcm = (a * b) / __gcd(a, b);
int ans = 0;
for ( int i = 0;
i < divisor.Length; i++)
{
int x = 0;
if (divisor[i] != 0)
x = (divisor[i] -
(a % divisor[i]));
if (x == 0)
continue ;
int product = (b + x) *
(a + x);
int tempGCD = __gcd(a + x,
b + x);
int tempLCM = product /
tempGCD;
if (lcm > tempLCM)
{
ans = x;
lcm = tempLCM;
}
}
Console.Write(ans);
}
public static void Main(String[] args)
{
int A = 6, B = 10;
findTheSmallestX(A, B);
}
}
|
Javascript
<script>
function __gcd(a,b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
function getDivisors(diff)
{
let divisor = [];
for (let i = 1;
i * i <= diff; i++)
{
if (i == diff / i)
{
divisor.push(i);
continue ;
}
if (diff % i == 0)
{
divisor.push(i);
divisor.push(diff / i);
}
}
let ans = new Array(divisor.length);
let j = 0;
for (let i=0;i< divisor.length;i++)
ans[i] = divisor[i];
return ans;
}
function findTheSmallestX(a,b)
{
let diff = b - a;
let divisor =
getDivisors(diff);
let lcm = (a * b) /
__gcd(a, b);
let ans = 0;
for (let i = 0;
i <divisor.length; i++)
{
let x = 0;
if (divisor[i] != 0)
x = (divisor[i] -
(a % divisor[i]));
if (x == 0)
continue ;
let product = (b + x) *
(a + x);
let tempGCD = __gcd(a + x,
b + x);
let tempLCM = product /
tempGCD;
if (lcm > tempLCM)
{
ans = x;
lcm = tempLCM;
}
}
document.write(ans);
}
let A = 6, B = 10;
findTheSmallestX(A, B);
</script>
|
Time Complexity: O(sqrt(B – A))
Auxiliary Space: O(max(A, B))
Share your thoughts in the comments
Please Login to comment...