Given two large numbers ‘a’ and ‘b’ such that(10^20<=a, b<=10^300). Find the LCM of two large numbers given.

**Examples:**

Input: a = 234516789234023485693020129 b = 176892058718950472893785940 Output: 41484157651764614525905399263631111992263435437186260 Input: a = 36594652830916364940473625749407 b = 448507083624364748494746353648484939 Output: 443593541011902763984944550799004089258248037004507648321189937329

**Solution:** In the given problem, we can see that the number are very large which is outside the limit of all available primitive data types, so we have to use the concept of BigInteger Class in Java. So we convert the given strings into biginteger and then we use java.math.BigInteger.gcd(BigInteger val) method to compute gcd of large numbers and then we calculate lcm using following formula:

LCM * HCF = x * y, where x and y are two numbers

Below is implementation of the above idea.

## Java

`// Java Program to find LCM of two large numbers ` `import` `java.math.*; ` `import` `java.lang.*; ` `import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// function to calculate LCM of two large numbers ` ` ` `public` `static` `BigInteger lcm(String a, String b) ` ` ` `{ ` ` ` `// convert string 'a' and 'b' into BigInteger ` ` ` `BigInteger s = ` `new` `BigInteger(a); ` ` ` `BigInteger s1 = ` `new` `BigInteger(b); ` ` ` ` ` `// calculate multiplication of two bigintegers ` ` ` `BigInteger mul = s.multiply(s1); ` ` ` ` ` `// calculate gcd of two bigintegers ` ` ` `BigInteger gcd = s.gcd(s1); ` ` ` ` ` `// calculate lcm using formula: lcm * gcd = x * y ` ` ` `BigInteger lcm = mul.divide(gcd); ` ` ` `return` `lcm; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// Input 'a' and 'b' are in the form of strings because ` ` ` `// they can not be handled by integer data type ` ` ` `String a = ` `"36594652830916364940473625749407"` `; ` ` ` `String b = ` `"448507083624364748494746353648484939"` `; ` ` ` ` ` `System.out.print(lcm(a, b)); ` ` ` `} ` `} ` `// Code contributed by Saurav Jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find LCM of two ` `# large numbers ` `import` `math ` ` ` `# Function to calculate LCM of two ` `# large numbers ` `def` `lcm (a, b): ` ` ` ` ` `# Convert string 'a' and 'b' ` ` ` `# into Integer ` ` ` `s ` `=` `int` `(a) ` ` ` `s1 ` `=` `int` `(b) ` ` ` ` ` `# Calculate multiplication of ` ` ` `# both integers ` ` ` `mul ` `=` `s ` `*` `s1 ` ` ` ` ` `# Calculate gcd of two integers ` ` ` `gcd ` `=` `math.gcd(s, s1) ` ` ` ` ` `# Calculate lcm using ` ` ` `# formula: lcm * gcd = x * y ` ` ` `lcm ` `=` `mul ` `/` `/` `gcd ` ` ` ` ` `return` `lcm ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Input 'a' and 'b' are in the ` ` ` `# form of strings ` ` ` `a ` `=` `"36594652830916364940473625749407"` ` ` `b ` `=` `"448507083624364748494746353648484939"` ` ` ` ` `print` `(lcm(a, b)) ` ` ` `# This code is contributed by himanshu77 ` |

*chevron_right*

*filter_none*

**Output:**

443593541011902763984944550799004089258248037004507648321189937329

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Multiply large integers under large modulo
- Finding LCM of more than two (or array) numbers without using GCD
- Program to find LCM of two numbers
- Program to find LCM of two Fibonnaci Numbers
- Find two numbers with the given LCM and minimum possible difference
- Program to find the LCM of two prime numbers
- Find two numbers with given sum and maximum possible LCM
- Find two distinct numbers such that their LCM lies in given range
- Minimum number to be added to minimize LCM of two given numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- GCD of two numbers when one of them can be very large
- Sum of two large numbers
- Last digit of Product of two Large or Small numbers (a * b)
- Sum of two large Floating-point numbers
- Subtraction of two large numbers using 9's compliment
- Subtraction of two large numbers using 10's compliment
- Difference of two large numbers
- Maximum sum of distinct numbers with LCM as N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.