javascript

findLCM()

Parameters: num1 (number), num2 (number)

Two numeric inputs for which LCM would be computed.

Returns: LCM of num1 and num2 (number)

This function calculates the Least Common Multiple (LCM) of two integers. The function utilizes the mathematical principle of division and Euclidean algorithm.

variables
conditionals
functions
arithmetic operations
loops
Medium dificulty

How to Write a JavaScript Function for Finding the Least Common Multiple

Hello there, fellow programmer! This blog post will walk you through the steps of programming a function in Javascript to find the Least Common Multiple (LCM) of two numbers. We'll keep the explanations clear and to the point, avoiding any confusing lingo. Get ready to add another handy function to your coding toolkit!

Step 1: Understanding the Problem

We need to create a function in JavaScript that takes in two numbers and returns their Lowest Common Multiple (LCM). The LCM of two integers a and b, usually denoted by lcm(a, b), is the smallest positive integer that is divisible by both a and b. So our task is to write a function called findLCM which takes two numbers as inputs and returns their LCM.

function findLCM(a, b) {
  // Our code will go here
}

Step 2: Identifying a Mathematical Helper

Since we're solving a mathematical problem, it's best to use mathematical properties. In this case, we can use the fact that “a*b = gcd(a,b)*lcm(a,b)”. So we can say that lcm(a, b) = (a * b) / gcd(a, b) where gcd(a, b) is the greatest common divisor of a and b.

function findLCM(a, b) {
  // To implement this we first need to find the gcd
  let gcd = findGCD(a, b);

  // Then we find the LCM
  let lcm = (a * b) / gcd;
  return lcm;
}

Step 3: Implementing the GCD Helper Function

We'll write a helper function called findGCD that takes in 2 numbers and returns their greatest common divisor. For this, we can use the Euclidean algorithm. The Euclidean algorithm is a way to find the greatest common divisor of two positive integers a and b. It starts with the division of a by b and takes the remainder r. Then, replace a by b and replace b by r, and repeat the division.

function findGCD(a, b) {
  if (b == 0) {
    return a;
  } else {
    return findGCD(b, a % b);
  }
}

Step 4: Integrating the GCD Helper Function

Now that we have our helper function to get the gcd, we can use it in our findLCM function.

function findGCD(a, b) {
  if (b == 0) {
    return a;
  } else {
    return findGCD(b, a % b);
  }
}

function findLCM(a, b) {
  let gcd = findGCD(a, b);
  let lcm = (a * b) / gcd;
  return lcm;
}

Step 5: Testing our function

Let's run some tests on our function to make sure it's working as expected.

console.log(findLCM(12, 15)); // Expected output: 60
console.log(findLCM(5, 10)); // Expected output: 10
console.log(findLCM(7, 5)); // Expected output: 35

In conclusion, we have successfully implemented a JavaScript function to find the Lowest Common Multiple of two given numbers by using the mathematical connection with greatest common divisor. This function can be used as a utility in many numeric or algebraic JavaScript applications. The final, fully-implemented function is as follows:

function findGCD(a, b) {
  if (b == 0) {
    return a;
  } else {
    return findGCD(b, a % b);
  }
}

function findLCM(a, b) {
  let gcd = findGCD(a, b);
  let lcm = (a * b) / gcd;
  return lcm;
}

Learn function in:

Least Common Multiple (LCM)

LCM finds the smallest number that's multiple of two or more integers.

Learn more

Mathematical principle

The Least Common Multiple (LCM) is the smallest number that is a multiple of both given numbers. In our function, we use the Euclidean algorithm, which states that the absolute value of the difference of two numbers is more conducive to finding LCM. According to the formula `LCM(a, b) = (a*b) / GCD(a, b)` where GCD is the greatest common divisor.

Learn more