MCM de dos números grandes

Dados dos números grandes ‘a’ y ‘b’ tales que (10^20<=a, b<=10^300). Encuentra el MCM de dos números grandes dados. Ejemplos:

 Input: a = 234516789234023485693020129
        b = 176892058718950472893785940
 Output: 41484157651764614525905399263631111992263435437186260

 Input: a = 36594652830916364940473625749407
        b = 448507083624364748494746353648484939
 Output: 443593541011902763984944550799004089258248037004507648321189937329

Solución: en el problema dado, podemos ver que el número es muy grande y está fuera del límite de todos los tipos de datos primitivos disponibles, por lo que debemos usar el concepto de clase BigInteger en Java. Así que convertimos las strings dadas en biginteger y luego usamos el método java.math.BigInteger.gcd(BigInteger val) para calcular mcd de números grandes y luego calculamos lcm usando la siguiente fórmula: LCM * HCF = x * y, donde x y y son dos números A continuación se muestra la implementación de la idea anterior. 

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

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

Javascript

// JavaScript program to find LCM of two
// large numbers
function __gcd(a, b)
{
    if (b == 0n)
        return a;
    return __gcd(b, a % b);
}
 
// Function to calculate LCM of two
// large numbers
function lcm (a, b)
{
 
    // Convert string 'a' and 'b'
    // into Integer
    let s = BigInt(a);
    let s1 = BigInt(b);
 
    // Calculate multiplication of
    // both integers
    let mul = s * s1;
 
    // Calculate gcd of two integers
    let gcd = __gcd(s, s1);
 
    // Calculate lcm using
    // formula: lcm * gcd = x * y
    let lcm = (mul - (mul % gcd)) / gcd;
 
    return lcm;
}
 
// Driver Code
 
//  Input 'a' and 'b' are in the
// form of strings
let a = "36594652830916364940473625749407";
let b = "448507083624364748494746353648484939";
 
console.log(lcm(a, b));
 
//  This code is contributed by phasing17

Producción:

443593541011902763984944550799004089258248037004507648321189937329

Publicación traducida automáticamente

Artículo escrito por Saurav Jain y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *