| Viewed 120 times

get the LCM of Two Numbers in Javascript


The LCM is the smallest integer that divides both numbers such that the remainder is zero.

1 Answer
Mohammad Qandeel
Answered
25

 

let lcm = (n1, n2) => {
  //Find the smallest and biggest number from both the numbers
  let lar = Math.max(n1, n2);
  let small = Math.min(n1, n2);
  
  //Loop till you find a number by adding the largest number which is divisble by the smallest number
  let i = lar;
  while(i % small !== 0){
    i += lar;
  }
  
  //return the number
  return i;
}

Example

lcm(11,21) //231



Related Questions