Skip to content

Instantly share code, notes, and snippets.

@martin-mok
Last active January 12, 2020 22:15
Show Gist options
  • Select an option

  • Save martin-mok/c4dbfe2b0bbcce60d2663aeb201bd338 to your computer and use it in GitHub Desktop.

Select an option

Save martin-mok/c4dbfe2b0bbcce60d2663aeb201bd338 to your computer and use it in GitHub Desktop.
freecodecamp: Smallest Common Multiple

Description

Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. The range will be an array of two numbers that will not necessarily be in numerical order. For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.

Tests

tests:
  - text: <code>smallestCommons([1, 5])</code> should return a number.
    testString: assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
  - text: <code>smallestCommons([1, 5])</code> should return 60.
    testString: assert.deepEqual(smallestCommons([1, 5]), 60);
  - text: <code>smallestCommons([5, 1])</code> should return 60.
    testString: assert.deepEqual(smallestCommons([5, 1]), 60);
  - text: <code>smallestCommons([2, 10])</code> should return 2520.
    testString: assert.deepEqual(smallestCommons([2, 10]), 2520);
  - text: <code>smallestCommons([1, 13])</code> should return 360360.
    testString: assert.deepEqual(smallestCommons([1, 13]), 360360);
  - text: <code>smallestCommons([23, 18])</code> should return 6056820.
    testString: assert.deepEqual(smallestCommons([23, 18]), 6056820);

Solution

Offical soln:

function gcd(a, b) {
    while (b !== 0) {
        a = [b, b = a % b][0];
    }
    return a;
}

function lcm(a, b) {
    return (a * b) / gcd(a, b);
}

function smallestCommons(arr) {
  arr.sort(function(a,b) {return a-b;});
  var rng = [];
  for (var i = arr[0]; i <= arr[1]; i++) {
    rng.push(i);
  }
  return rng.reduce(lcm);
}

The offical soln used the Euclidean Algorithm. Below is an iterative method to check for common multiple integer starting from 1:
Smallest Common Multiple - Hugh Winchester
FreeCodeCamp: Intermediate Algorithm Scripting: Smallest Common Multiple

function smallestCommons(arr) {
  let min = Math.min(...arr);
  let max = Math.max(...arr);
  let commonMultiple=1;
  while (true) {
      ++commonMultiple;
      for (let i = min; i <= max; ++i){
        if (commonMultiple % i !== 0){
          break;
        }
        if(i===max) {
          return commonMultiple;
        }
      }
  }
}

A simplier and elegant soln provided by JanEgbers:

function smallestCommons(arr) {

  var max = Math.max(arr[0], arr[1]);
  var min = Math.min(arr[0], arr[1]);
  var mltple = max;

  for(var i = max; i >= min; i--){
    if(mltple % i !== 0){
      mltple += max; 
      i = max;
    } 
  }

  return mltple;  
}

JanEgbers soln is based on method at section 2.2 Finding the Least Common Multiple in Java on Baeldung
A more advanced approach uses Prime Factorization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment