Given two large numbers ‘a’ and ‘b’ such that(10^20<=a, b<=10^300). Find the LCM of two large numbers given.
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.
- Sum of two large numbers
- GCD of two numbers when one of them can be very large
- Difference of two large numbers
- Remainder with 7 for large numbers
- Divisible by 37 for large numbers
- Find Last Digit of a^b for Large Numbers
- Sum of first N natural numbers when N is extremely large
- Large Fibonacci Numbers in Java
- Sum of all natural numbers from L to R ( for large values of L and R )
- Sort an array of large numbers
- Multiply Large Numbers using Grid Method
- Writing power function for large numbers
- Last digit of Product of two Large or Small numbers (a * b)
- Multiply Large Numbers represented as Strings
- Modulo power for large numbers represented as strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.