Project Euler - Problem 5 - Smallest Multiple: Coding Interview Prep (freeCodeCamp)

 Problem 5: Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to n?


Solution:


function smallestMult(n) {

  let lcm = 1;

  for(let i=2;i<=n;i++) {
    lcm = LCM(lcm,i)
  }
  console.log(lcm)

  return lcm;
}

//Euclidean recursive algorithm
function GCD(x,y) {
  if(y === 0return x;
  return GCD(yx%y);
}

function LCM(x,y) {
  return x*y/GCD(x,y)
}

smallestMult(10);


Click here to go to the original link of the question.

Post a Comment

Previous Post Next Post