JavaScript 查找两个数的最小公倍数(LCM)的程序

JavaScript 查找两个数的最小公倍数(LCM)的程序

在本文中,我们将通过使用JavaScript来学习如何查找两个数的最小公倍数。两个数的最小公倍数是能够整除这两个数且没有余数的最小正整数。它是这两个数的公共倍数。

 LCM of two numbers = Product of two numbers ÷ HCF of two numbers.  
 LCM(a, b) = (a * b) / GCD(a, b)  
Where GCD(a, b) represents the Greatest Common Divisor of the two numbers.

示例:

Numbers : 12 and 80  
prime factors of each  
12: 2 × 2 × 3    
80: 2 × 2 × 2 × 2 × 5  (2: 4 times,3: 1 time,5: 1 time)  
Multiply each factor the maximum number of times it occurs in either number.  
2 x 2 x 2 x 2 x 3 x 5 =240  ( 240 is the lowest number that can be divided by both 12 and 80.)

有几种方法可以用来检查一个数是奇数还是偶数,如下所示:

  • 使用公式 (a * b) / GCD(a, b)
  • 不使用公式
  • 使用循环

我们将探讨上述所有方法以及它们的基本实现,带有示例的帮助。

使用公式 (a * b) / GCD(a, b) 查找两个数的最小公倍数的 JavaScript 程序

在这种方法中,使用公式 (a * b) / GCD(a, b)。它使用欧几里得算法计算最大公约数 (GCD),然后应用该公式来确定最小公倍数 (LCM)。这确保获得的 LCM 是这两个输入数都能整除的最小倍数。

语法:

function lcmFunction( a, b ) {  
    const gcdValue = gcd( a, b );  
    return (a * b) / gcdValue;  
}

示例: 在这个例子中,使用循环计算最大公约数(GCD)。最小公倍数(LCM)通过公式确定。准确的LCM结果确保了12和18两个数的可整除性。

JavaScript

function gcd(a, b) { 
    for (let temp = b; b !== 0;) { 
        b = a % b; 
        a = temp; 
        temp = b; 
    } 
    return a; 
} 
  
function lcmFunction(a, b) { 
    const gcdValue = gcd(a, b); 
    return (a * b) / gcdValue; 
} 
  
let num1 = 12; 
let num2 = 18; 
let lcm = lcmFunction(num1, num2); 
console.log("LCM of given numbers is :", lcm);

输出

LCM of given numbers is : 36

JavaScript程序查找两个数字的最小公倍数而不使用公式

在这种方法中,我们没有使用最大公约数(GCD)来查找两个数字的最小公倍数。它从较大的数字开始迭代,并通过较小的数字来检查可整除性,从而提供准确的最小公倍数。

语法:

function findLCM(a, b) {  
    let lar = Math.max(a, b);  
    let small = Math.min(a, b);  
    for (i = lar; ; i += lar) {  
        if (i % small == 0)  
            return i;  
    }  
};

示例: 在此示例中,我们将使用上述解释的方法。

JavaScript

// JavaScript program to find LCM of 2 numbers 
// without using GCD 
  
// Function to return LCM of two numbers 
function findLCM(a, b) { 
    let lar = Math.max(a, b); 
    let small = Math.min(a, b); 
    for (i = lar; ; i += lar) { 
        if (i % small == 0) 
            return i; 
    } 
} 
  
// Driver program to test above function 
let a = 5, b = 7; 
console.log("LCM of " + a + " and " +  
    b + " is " + findLCM(a, b));

输出

LCM of 5 and 7 is 35

JavaScript程序使用循环查找两个数的最小公倍数

在这种方法中,我们使用循环来查找两个数的最小公倍数。从较大的数开始,它以这个值递增,直到找到一个可以被较小的数整除的倍数,从而得到最小公倍数。

语法:

function lcmFunction(a, b) {  
    let larger = Math.max(a, b);  
    let smaller = Math.min(a, b);  
    for (let i = larger; ; i += larger) {  
        if (i % smaller === 0) {  
            return i;  
        }  
    }  
};

示例: 在这个例子中,我们使用了上述解释的方法。

JavaScript

function lcmFunction(a, b) { 
    let larger = Math.max(a, b); 
    let smaller = Math.min(a, b); 
    for (let i = larger; ; i += larger) { 
        if (i % smaller === 0) { 
            return i; 
        } 
    } 
} 
  
let num1 = 12; 
let num2 = 18; 
let result = lcmFunction(num1, num2); 
console.log(`LCM of {num1} and{num2} is ${result}`);

输出

LCM of 12 and 18 is 36

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程