Minimum possible value of max(A, B) such that LCM(A, B) = C
Given an integer C, the task is to find the minimum possible value of max(A, B) such that LCM(A, B) = C.
Examples:
Input: C = 6
Output: 3
max(1, 6) = 6
max(2, 3) = 3
and min(6, 3) = 3
Input: C = 9
Output: 9
Approach: An approach to solve this problem is to find all the factors of the given number using the approach discussed in this article and then find the pair (A, B) that satisfies the given conditions and take the overall minimum of the maximum of these pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lcm( int a, int b)
{
return (a / __gcd(a, b) * b);
}
int getMinValue( int c)
{
int ans = INT_MAX;
for ( int i = 1; i <= sqrt (c); i++) {
if (c % i == 0 && lcm(i, c / i) == c) {
ans = min(ans, max(i, c / i));
}
}
return ans;
}
int main()
{
int c = 6;
cout << getMinValue(c);
return 0;
}
|
Java
class Solution
{
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int lcm( int a, int b)
{
return (a / __gcd(a, b) * b);
}
static int getMinValue( int c)
{
int ans = Integer.MAX_VALUE;
for ( int i = 1 ; i <= Math.sqrt(c); i++)
{
if (c % i == 0 && lcm(i, c / i) == c)
{
ans = Math.min(ans, Math.max(i, c / i));
}
}
return ans;
}
public static void main(String args[])
{
int c = 6 ;
System.out.println(getMinValue(c));
}
}
|
Python3
import sys
def __gcd(a, b):
if (a = = 0 ):
return b;
if (b = = 0 ):
return a;
if (a = = b):
return a;
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
def lcm(a, b):
return (a / __gcd(a, b) * b);
def getMinValue(c):
ans = sys.maxsize;
for i in range ( 1 , int ( pow (c, 1 / 2 )) + 1 ):
if (c % i = = 0 and lcm(i, c / i) = = c):
ans = min (ans, max (i, c / i));
return int (ans);
if __name__ = = '__main__' :
c = 6 ;
print (getMinValue(c));
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int lcm( int a, int b)
{
return (a / __gcd(a, b) * b);
}
static int getMinValue( int c)
{
int ans = int .MaxValue;
for ( int i = 1; i <= Math.Sqrt(c); i++)
{
if (c % i == 0 && lcm(i, c / i) == c)
{
ans = Math.Min(ans, Math.Max(i, c / i));
}
}
return ans;
}
public static void Main()
{
int c = 6;
Console.WriteLine(getMinValue(c));
}
}
|
Javascript
<script>
function __gcd(a , b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function lcm(a , b)
{
return (a / __gcd(a, b) * b);
}
function getMinValue(c)
{
var ans = Number.MAX_VALUE;
for (i = 1; i <= Math.sqrt(c); i++)
{
if (c % i == 0 && lcm(i, c / i) == c)
{
ans = Math.min(ans, Math.max(i, c / i));
}
}
return ans;
}
var c = 6;
document.write(getMinValue(c));
</script>
|
Last Updated :
18 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...