Find Lcm Algorithm

The Find LCM (Least Common Multiple) Algorithm is a mathematical technique used to determine the smallest multiple that two or more numbers share. The LCM of a set of numbers is the smallest positive integer that is evenly divisible by all numbers in the set. The algorithm is commonly employed in solving problems that involve finding common periods, common denominators, or synchronization points in repeating events. It is an essential tool in number theory, algebra, and other fields of mathematics. There are several methods to find the LCM of a set of numbers, including the prime factorization method, the division method, and the consecutive integer checking method. One of the most efficient ways is using the prime factorization method, which involves breaking down each number into its prime factors and then combining the greatest powers of all the factors to form the LCM. Another efficient approach is using the GCD (Greatest Common Divisor) method, where the LCM of two numbers can be found by dividing the product of the numbers by their GCD. This method can be easily extended to find the LCM of more numbers by iteratively applying the LCM-GCD formula for pairs of numbers in the set. These algorithms are widely used in computer science and programming, as they provide a fast and efficient way of calculating the LCM for various applications.
/*
    author: PatOnTheBack
    license: GPL-3.0 or later

    Modified from:
        https://github.com/TheAlgorithms/Python/blob/master/maths/findLcm.py

    More about LCM:
        https://en.wikipedia.org/wiki/Least_common_multiple
*/

'use strict'

// Find the LCM of two numbers.
function findLcm (num1, num2) {
  var maxNum
  var lcm
  // Check to see whether num1 or num2 is larger.
  if (num1 > num2) {
    maxNum = num1
  } else {
    maxNum = num2
  }
  lcm = maxNum

  while (true) {
    if ((lcm % num1 === 0) && (lcm % num2 === 0)) {
      break
    }
    lcm += maxNum
  }
  return lcm
}

// Run `findLcm` Function
var num1 = 12
var num2 = 76
console.log(findLcm(num1, num2))

LANGUAGE:

DARK MODE: