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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to return the LCM of a and bint lcm(int a, int b){ return (a / __gcd(a, b) * b);}// Function to return the minimized valueint getMinValue(int c){ int ans = INT_MAX; // To find the factors for (int i = 1; i <= sqrt(c); i++) { // To check if i is a factor of c and // the minimum possible number // satisfying the given conditions if (c % i == 0 && lcm(i, c / i) == c) { // Update the answer ans = min(ans, max(i, c / i)); } } return ans;}// Driver codeint main(){ int c = 6; cout << getMinValue(c); return 0;} |
Java
// Java implementation of the approachclass Solution{ // Recursive function to return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to return the LCM of a and b static int lcm(int a, int b) { return (a / __gcd(a, b) * b); } // Function to return the minimized value static int getMinValue(int c) { int ans = Integer.MAX_VALUE; // To find the factors for (int i = 1; i <= Math.sqrt(c); i++) { // To check if i is a factor of c and // the minimum possible number // satisfying the given conditions if (c % i == 0 && lcm(i, c / i) == c) { // Update the answer ans = Math.min(ans, Math.max(i, c / i)); } } return ans; } // Driver code public static void main(String args[]) { int c = 6; System.out.println(getMinValue(c)); }}// This code is contributed by Arnab Kundu |
Python3
# Python implementation of the approachimport sys# Recursive function to return gcd of a and bdef __gcd(a, b): # Everything divides 0 if (a == 0): return b; if (b == 0): return a; # base case if (a == b): return a; # a is greater if (a > b): return __gcd(a - b, b); return __gcd(a, b - a);# Function to return the LCM of a and bdef lcm(a, b): return (a / __gcd(a, b) * b);# Function to return the minimized valuedef getMinValue(c): ans = sys.maxsize; # To find the factors for i in range(1, int(pow(c, 1/2)) + 1): # To check if i is a factor of c and # the minimum possible number # satisfying the given conditions if (c % i == 0 and lcm(i, c / i) == c): # Update the answer ans = min(ans, max(i, c / i)); return int(ans);# Driver codeif __name__ == '__main__': c = 6; print(getMinValue(c)); # This code is contributed by 29AjayKumar |
C#
// C# implementation of the approach using System;class GFG{ // Recursive function to return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to return the LCM of a and b static int lcm(int a, int b) { return (a / __gcd(a, b) * b); } // Function to return the minimized value static int getMinValue(int c) { int ans = int.MaxValue; // To find the factors for (int i = 1; i <= Math.Sqrt(c); i++) { // To check if i is a factor of c and // the minimum possible number // satisfying the given conditions if (c % i == 0 && lcm(i, c / i) == c) { // Update the answer ans = Math.Min(ans, Math.Max(i, c / i)); } } return ans; } // Driver code public static void Main() { int c = 6; Console.WriteLine(getMinValue(c)); } } // This code is contributed by AnkitRai01 |
Javascript
<script>// javascript implementation of the approach// Recursive function to return gcd of a and b function __gcd(a , b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); }// Function to return the LCM of a and bfunction lcm(a , b){ return (a / __gcd(a, b) * b);}// Function to return the minimized valuefunction getMinValue(c){ var ans = Number.MAX_VALUE; // To find the factors for (i = 1; i <= Math.sqrt(c); i++) { // To check if i is a factor of c and // the minimum possible number // satisfying the given conditions if (c % i == 0 && lcm(i, c / i) == c) { // Update the answer ans = Math.min(ans, Math.max(i, c / i)); } } return ans;}// Driver codevar c = 6;document.write(getMinValue(c));// This code contributed by shikhasingrajput </script> |
Output:
3
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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



