# freeCodeCamp Challenge Guide: Smallest Common Multiple

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 11:19pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075 "2016-06-30T23:19:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 11:19pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/1 "2016-06-30T23:19:51Z")

</div>

# [Smallest Common Multiple](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple)

* * *

## Problem Explanation

The smallest common multiple between two numbers is the smallest number that both numbers can divide into evenly. This concept can be extended to more than two numbers as well.

We can first start with finding the smallest common multiple between two numbers. Naively, we can start writing out multiple of each number until we write a multiple that exists from both numbers.

An example would be the numbers `3` and `4`. The multiples of `3` are `3, 6, 9, 12, 15, 18, ...` and the multiples of `4` are `4, 8, 12, 16, 20, ...`. The first smallest number we run into in both lists is `12` so this is the smallest common multiple between `3` and `4`.

An faster approach is to check all multiples of `4` to see if they are also multiples of `3`, by checking the remainder when we divide the multiple of `4` by `3`.

Be careful - do not forget the keyword **range**. If we are given `[1, 5]`, then we have to check for the smallest common multiple for all the numbers `[1, 2, 3, 4, 5]`, which is the smallest number that is evenly divisible by all of them.

#### Relevant Links

- [Least (Smallest) Common Multiple](https://en.wikipedia.org/wiki/Least_common_multiple)

- [Greatest Common Divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor#greatest_common_denominator)

* * *

## Hints

### Hint 1

You can use remainder operator (`%`) to check if the reminder of a division is 0, which means it is evenly divisible.

* * *

## Solutions

> **Solution 1 - Looping approach (Click to Show/Hide)**
>
> ```auto
> function smallestCommons(arr) {
> // Setup
> const [min, max] = arr.sort((a, b) => a - b);
> const numberDivisors = max - min + 1;
> // Largest possible value for SCM
> let upperBound = 1;
> for (let i = min; i <= max; i++) {
> upperBound *= i;
> }
> // Test all multiples of 'max'
> for (let multiple = max; multiple <= upperBound; multiple += max) {
> // Check if every value in range divides 'multiple'
> let divisorCount = 0;
> for (let i = min; i <= max; i++) {
> // Count divisors
> if (multiple % i === 0) {
> divisorCount += 1;
> }
> }
> if (divisorCount === numberDivisors) {
> return multiple;
> }
> }
> }
> 
> smallestCommons([1, 5]);
> 
> ```
> 
> In this solution, we check every multiple of the largest value in the range until we find a value that is divisible by every value in the range.
> 
> The upper bound for this loop is the product of all values in the provided range, because this number will be divisible by every value in the range.

> **Solution 2 - ES6 looping (Click to Show/Hide)**
>
> ```auto
> function smallestCommons(arr) {
> // Setup
> const [min, max] = arr.sort((a, b) => a - b);
> const range = Array(max - min + 1)
> .fill(0)
> .map((_, i) => i + min);
> // Largest possible value for SCM
> const upperBound = range.reduce((prod, curr) => prod * curr);
> // Test all multiples of 'max'
> for (let multiple = max; multiple <= upperBound; multiple += max) {
> // Check if every value in range divides 'multiple'
> const divisible = range.every((value) => multiple % value === 0);
> if (divisible) {
> return multiple;
> }
> }
> }
> 
> smallestCommons([1, 5]);
> 
> ```
> 
> This solution uses ES6 syntax to condense the logic in Solution 1.

> **Solution 3 - GCD and LCM (Click to Show/Hide)**
>
> ```auto
> function smallestCommons(arr) {
> // Setup
> const [min, max] = arr.sort((a, b) => a - b);
> const range = Array(max - min + 1)
> .fill(0)
> .map((_, i) => i + min);
> // GCD of two numbers
> // https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm
> const gcd = (a, b) => (b === 0) ? a : gcd(b, a % b);
> // LCM of two numbers
> // https://en.wikipedia.org/wiki/Least_common_multiple#Using_the_greatest_common_divisor
> const lcm = (a, b) => a * b / gcd(a, b);
> // LCM of multiple numbers
> // https://en.wikipedia.org/wiki/Least_common_multiple#Other
> return range.reduce((multiple, curr) => lcm(multiple, curr));
> }
> 
> smallestCommons([1, 5]);
> 
> ```
> 
> This solution uses formulae and algorithms for the Greatest Common Divisor and Least Common Multiple off of Wikipedia to compactly and quickly compute the Smallest Common Multiple.

> **Solution 4 - Prime factorization (Click to Show/Hide)**
>
> ```auto
> // Find the SCM of a range of numbers
> function smallestCommons(arr) {
> const primeFactors = {};
> const [min, max] = arr.sort((a, b) => a - b);
> for (let i = min; i <= max; i++) {
> // Factorize number in range
> const currentFactors = getPrimeFactors(i);
> for (let prime in currentFactors) {
> // Add factor to set or update number of occurrences
> if (!primeFactors[prime] || currentFactors[prime] > primeFactors[prime]) {
> primeFactors[prime] = currentFactors[prime]
> }
> }
> }
> // Build SCM from factorization
> let multiple = 1;
> for (let prime in primeFactors) {
> multiple *= prime ** primeFactors[prime];
> }
> return multiple;
> }
> 
> // Compute prime factors of a number
> function getPrimeFactors(num) {
> const factors = {};
> for (let i = 2; i <= num; i++) {
> // Count occurances of factor
> // Note that composite values will not divide num
> while ((num % i) === 0) {
> factors[i] = (factors[i]) ? factors[i] + 1 : 1;
> num /= i;
> }
> }
> return factors;
> }
> 
> smallestCommons([1, 5]);
> 
> ```
> 
> This solution uses a prime factorization of the numbers in the range to compute the smallest common multiple. In general, Solution 3 is much faster, but with a very large range or very large values, sometimes Solution 3 may trigger a recursion limit in some browsers.

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 10, 2016, 2:59am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/2 "2016-08-10T02:59:46Z")

</div>

Closed on August 10, 2016, 2:59am UTC



---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 20, 2016, 8:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/3 "2016-08-20T20:26:37Z")

</div>

Opened on August 20, 2016, 8:26pm UTC



---

<div class="post-metadata">

**Author:** ![samuelpath](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/samuelpath/32/162094_2.png) [@samuelpath](https://forum.freecodecamp.org/u/samuelpath)\
**Post date:** [September 1, 2016, 10:11am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/4 "2016-09-01T10:11:14Z")

</div>

You forgot the indentation on the intermediate and advanced solution, making them harder to read.  
Can I correct this myselft or should I ask you to update it?  
Thx again.

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [September 1, 2016, 7:12pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/5 "2016-09-01T19:12:42Z")

</div>

You can do it yourself.

---

<div class="post-metadata">

**Author:** ![drguildo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/drguildo/32/161905_2.png) [@drguildo](https://forum.freecodecamp.org/u/drguildo)\
**Post date:** [December 13, 2016, 11:36am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/6 "2016-12-13T11:36:47Z")

</div>

```auto
function smallestCommons(arr) {
  function isValidMultiple(m, min, max) {
    for (var i = min; i < max; i++) {
      if (m % i !== 0) {
        return false;
      }
    }
    
    return true;
  }
  
  var max = Math.max(arr[0], arr[1]);
  var min = Math.min(arr[0], arr[1]);
  var multiple = max;
  
  while (!isValidMultiple(multiple, min, max)) {
    multiple += max;
  }
  
  return multiple;
}

```

---

<div class="post-metadata">

**Author:** ![akashkriplani](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@akashkriplani](https://forum.freecodecamp.org/u/akashkriplani)\
**Post date:** [January 10, 2017, 7:00am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/7 "2017-01-10T07:00:37Z")

</div>

Alternative basic solution:

```
function smallestCommons(arr) {
  var min = Math.min.apply(null, arr);
  var max = Math.max.apply(null, arr);
  var listOfNum =[];
  while (min<=max){
    
      listOfNum.push(min);
      min++;
  }
  
  var result = listOfNum[0];
  
for (var i = 1; i<listOfNum.length; i++)
    result = findLCM(result, listOfNum[i]);
  
  return result;
}

function gcd(a, b){
  
  while (a !== b){
    
    if(a > b)
      a = a - b;
    else
      b = b - a;
    
  }

  return a; // GCD of two numbers
}

function findLCM(a, b){
  
  return a * (b / gcd (a,b));
  
}
smallestCommons([1,5]);
```

---

<div class="post-metadata">

**Author:** ![Cowwy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cowwy/32/158666_2.png) [@Cowwy](https://forum.freecodecamp.org/u/Cowwy)\
**Post date:** [February 12, 2017, 2:31am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/8 "2017-02-12T02:31:52Z")

</div>

Dang, after reading all these solution. I just realized I tackled this problem the hardest way possible. LOL

---

<div class="post-metadata">

**Author:** ![JanEgbers](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janegbers/32/93172_2.png) [@JanEgbers](https://forum.freecodecamp.org/u/JanEgbers)\
**Post date:** [February 16, 2017, 1:04pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/9 "2017-02-16T13:04:21Z")

</div>

Came up with a short basic solution:

```
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;  
}
```

---

<div class="post-metadata">

**Author:** ![awb](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@awb](https://forum.freecodecamp.org/u/awb)\
**Post date:** [February 22, 2017, 6:07am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/10 "2017-02-22T06:07:28Z")

</div>

```auto
var tester = 0;

function smallestCommons(arr) {
var ultimate = 0;
  arr = arr.sort(function(a, b) {
    return a - b;
  });

  console.log(arr);

  var ranges = Range(arr[0], arr[1]);

  console.log(ranges);

  var first = arr[1];

  var final = first;

  ///function will run here
  recurse(first);

  function recurse(current) {

    var temp = 0;
    var tester = 0;
    for (i = arr[0]; i <= arr[1]; i++) {

      if (current % i === 0) {
        console.log(current + " % " + i);
        tester++;
        console.log(i + " works");
        console.log(tester + " tester");

      } else {

        console.log(i + " broke it");

        var counter = current + first;
        
        console.log(temp + " next run");
        recurse(counter);

        break;
      } //else

      console.log(ranges.length + " is length");

      if (tester === ranges.length) {
        console.log(current);
         ultimate = current;
      }

    } //for

  } //recurse
return ultimate;
} //smallest commons

function Range(a, b) {
  var temrange = [];

  for (i = a; i <= b; i++) {

    temrange.push(i);
  }
  return temrange;
}

smallestCommons([5, 1]);

```

heres my long and messy code that crashes my browser if the numbers are too big, it works for 1,5 but crashes for 1,13.

---

<div class="post-metadata">

**Author:** ![shengdzhang](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@shengdzhang](https://forum.freecodecamp.org/u/shengdzhang)\
**Post date:** [March 13, 2017, 1:01pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/11 "2017-03-13T13:01:23Z")

</div>

> function smallestCommons(arr) {

> ```
> var maxPrimes = {};
> 
> var min = Math.min(arr[0], arr[1]);
> var max = Math.max(arr[0], arr[1]);
> 
> var primeFactors = function (num) {
> var primeProd = {};
> var factor = 2;
> 
> if (num < 2) {
> return {};
> } 
> 
> while(factor <= num) {
> if(num % factor === 0){
> primeProd[factor] = primeProd[factor] + 1 || 1;
> num /= factor;
> } else {
> factor++;
> }
> }
> 
> return primeProd;
> };
> 
> //loop through numbers and save the most instance of the prime factors in the main hash 
> for(var i = min; i <= max; i++) {
> var temp = primeFactors(i);
> for(var key in temp) {
> if(!(key in maxPrimes) || maxPrimes[key] < temp[key]) {
> maxPrimes[key] = temp[key];
> }
> }
> }
> 
> var prod = 1;
> //loop through main hash to perform multiplication of all prime factors
> 
> for(var num in maxPrimes) {
> prod *= Math.pow(parseInt(num), maxPrimes[num]);
> }
> 
> return prod;
> 
> ```
> 
> }

Here’s what I ended up. Not optimized. The brute force method that is available right now was inefficient and broke down at [1, 23]. Works by making a hash of prime factors for each number and calculating the SCM through a main hash.

---

<div class="post-metadata">

**Author:** ![RaneWrites](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ranewrites/32/44163_2.png) [@RaneWrites](https://forum.freecodecamp.org/u/RaneWrites)\
**Post date:** [March 19, 2017, 5:55am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/12 "2017-03-19T05:55:46Z")

</div>

I factored all the numbers in the range and then multiplied them all together.

```
function smallestCommons(arr) {
  
  var allFactors = { };
  
  var tmpArr = [];
  
  var curNum;
  
  var factorKeys = {};
  
  var count = 0;
  
  var theProduct = 1;
  
  var tmpNum;
  
  if (arr[1] < arr[0]) {
    tmpNum = arr[1];
    arr[1] = arr[0];
    arr[0] = tmpNum;
  }
  
  for (var i = arr[0] > 1 ? arr[0] : 2, l = arr[1]; i <= l; i++) {
    tmpArr = factorMe(i);
    for (var j = 0, l2 = tmpArr.length; j < l2; j++) {
      if (!curNum || curNum != tmpArr[j]) {
        curNum = tmpArr[j];
        count = countNumbers(curNum, tmpArr);
        if (!allFactors[curNum] || allFactors[curNum] < count) {
          allFactors[curNum] = count;
        }
      }
    }
  }
  
  factorKeys = Object.keys(allFactors);

  for (var k = 0, l3 = factorKeys.length; k < l3; k++) {
    theProduct *= Math.pow( factorKeys[k], allFactors[factorKeys[k]] );
  }
 
  return theProduct;
}

function countNumbers(num, arr) {
  var count = 0;
  for (var i = 0, l = arr.length; i < l; i++) {
    if (num == arr[i]) count++;
  }
  return count;
}

function factorMe(num) {
  var factors = [];
  
  var divisor = 2;
  
  while(divisor != num) {
    if ( num % divisor === 0) {
      factors.push(divisor);
      num /= divisor;
    } else {
    divisor++;
    }
  }
  
  factors.push(num);

  
  return factors;
}

smallestCommons([1,5]);
```

---

<div class="post-metadata">

**Author:** ![zcabez](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zcabez/32/163129_2.png) [@zcabez](https://forum.freecodecamp.org/u/zcabez)\
**Post date:** [March 20, 2017, 6:32pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/13 "2017-03-20T18:32:07Z")

</div>

I have done it by getting the highest common multiple first then simplify it.

```
function smallestCommons(arr) {
  var sorted = arr.sort(),
      res = sorted[1],
      multiplier = sorted[1] - 1,
      simplifier = [2, 3, 5, 7],
      leastSearch = true;
 
 // Get the highest common multiple
  while(leastSearch){
    for(var i = sorted[0]; i <= sorted[1]; i++){
      
      if((res % i) !== 0){
        leastSearch = true;
        break;
      } 
      
      leastSearch = false;
    }
    
    if(leastSearch){
      res *= multiplier;
      multiplier--;
    }
    
  }
  
// Simplify the value
  for(var j = 0; j < simplifier.length; j++){
    var canBeSimplified = true;
    var tempRes;
    
    do {
      tempRes = res / simplifier[j];
      for(var x = sorted[0]; x <= sorted[1]; x++ ){
        if(tempRes % x !== 0){
          canBeSimplified = false;
          tempRes = tempRes * simplifier[j];
          break;
        }
      }
      
      res = tempRes;
    } while (canBeSimplified);
    
  }
  
  
  return res;
}

smallestCommons([1, 13]);
```

---

<div class="post-metadata">

**Author:** ![aosdab](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@aosdab](https://forum.freecodecamp.org/u/aosdab)\
**Post date:** [March 25, 2017, 11:35pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/14 "2017-03-25T23:35:43Z")

</div>

Simple and elegant. Well done

---

<div class="post-metadata">

**Author:** ![yodist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yodist/32/163076_2.png) [@yodist](https://forum.freecodecamp.org/u/yodist)\
**Post date:** [March 26, 2017, 1:09pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/15 "2017-03-26T13:09:51Z")

</div>

Below is how I pass this challenge, **fast and simple** :

```
function smallestCommons(arr) {
  var min=arr[0], max=arr[1], temp;
  if (min>max) {temp=min;min=max;max=temp;}
  var a, b, lim=min;
  for(var i=min;i<max;i++) {
    a=lim;
    b=i+1;
    for (lim; lim%b!==0 ;lim+=a);
  }
  
  return lim;
}

smallestCommons([1,5]);

```

[Run Code](https://repl.it/GehH/0)

---

<div class="post-metadata">

**Author:** ![chrishdez02](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@chrishdez02](https://forum.freecodecamp.org/u/chrishdez02)\
**Post date:** [March 30, 2017, 5:17am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/16 "2017-03-30T05:17:29Z")

</div>

I was able to pass this challenge with this piece of code. Not as efficient as some others I’ve seen here but glad that it works. I’m still new to this whole javascript thing and always looking for ways to make my code more efficient so any feedback is welcome :).

function smallestCommons(arr) {

var newArr = []  
var acc = Math.min(…arr);  
var firstVal = arr[0];  
var secondVal = arr[1];  
var boo = 0;

// Sequential Array

```
 while (acc< Math.max(...arr)+1) {

    newArr.push(acc);
    acc += 1;
  } 

```

while (boo == 0){  
while (firstVal != secondVal)  
{  
if (firstVal\>secondVal) {  
secondVal += arr[1];  
}

```
        else if (firstVal<secondVal) {    
          firstVal += arr[0];
        }

    }   

  for (i=1;i<newArr.length-1;i++) {

    if (firstVal % newArr[i] != 0) {  
      
      firstVal += arr[0];
      break;  
    }
    else if (i <newArr.length -2){
      continue; 
    }   
 
    else if (i = newArr.length-2) {
      var boo = 1; 
      continue;
    }
} 

```

}

return firstVal;

}  
smallestCommons([1,13]);

---

<div class="post-metadata">

**Author:** ![MagicMixer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/magicmixer/32/48095_2.png) [@MagicMixer](https://forum.freecodecamp.org/u/MagicMixer)\
**Post date:** [April 3, 2017, 10:12pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/17 "2017-04-03T22:12:47Z")

</div>

Advanced solution.  
This code is quite long, and firstly I thought I tackled this problem the hardest way possible, but after some testing I notices it can astonishingly compute the Smallest Common Multiple (SMC) of [1, 5000] in a few seconds…

```
// Check if value is a prime number
function isPrime (value) {
  
  if(value%2 === 0 && value > 2)
    return false;
  
  var halfValue = (value-1)/2;
  
  for(var i = 3; i < halfValue; i+=2)
    if(value%i === 0) 
      return false;
  
  return true;
}

// Give the prime factorization of a number
function primeFactors (value) {
  var i = 2;
  var factors = {};
  
  while( value >= i ) 
  {
    if( isPrime(i) ) 
    {
      while( value % i === 0 ) 
      {
        value /= i;
        factors[i] = factors[i] ? factors[i] + 1 : 1; 
      }
      i++; 
    }
    else 
      i++; 
  }
  return factors;
}

// Give the highest value of each factor of 2 primes factorization
function addFactors (f1, f2) {
  Object.keys(f2).forEach 
  (
    function(key) 
    {
      if( ( f1[key] || 0 ) < f2[key] )
        f1[key] = f2[key]; 
    } 
  );
  return f1;
}

// Calculate the smallest common multiple
function smallestCommons(arr) {
  var scm = 1;
  var factors = {};
  
  var min = Math.max( Math.min( arr[0], arr[1] ), 2 );
  var max = Math.max( arr[0], arr[1] );
  
  for(var i = min; i <= max; i++) 
  {
    factors = addFactors(factors,primeFactors(i)); 
  }
  
  Object.keys(factors).forEach 
  (
    function(key) {
      scm *= Math.pow(key, factors[key]); 
    } 
  );
  
  return scm; 
}

console.log(smallestCommons([1,5000]));

```

So how does it works ?

This code is based on the Prime Factorization (PF) of numbers  
e.g : 720 = 2x2x2x2 x3x3 x5

Let’s see the PF of the SMC of [1, 9] :

- 2520 = 2 x 2 x 2 x 3 x 3 x 5 x 7  
This can be computed with the function primeFactors(2520), which returns  
= \> { 2: 3 , 3: 2 , 5: 1 , 7: 1 }

The we do the PF of all numbers from 1 to 9 :

```
n 2 3 5 7  
---------------------
2 = 1 | | |  
3 = | 1 | |      
4 = 2 | | |     
5 = | | 1 |  
6 = 1 1 | |  
7 = | | | 1   
8 = 3 | | |  
9 = | 2 | |  
---------------------
2520 = 3 2 1 1

```

We can notice that each value of PF(2520) is the highest value possible in all PF from 1 to 9

The function addFactors return the PF with all the highest possible values.  
Then all that is left to do is the inverse of PF.

Fell free to correct / improve my english, it’s quite difficult to explain in another langage.

---

<div class="post-metadata">

**Author:** ![MaomaoFE](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@MaomaoFE](https://forum.freecodecamp.org/u/MaomaoFE)\
**Post date:** [April 20, 2017, 7:41am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/18 "2017-04-20T07:41:09Z")

</div>

I think the `product` should instand of `quot` in Basic Code Solution

---

<div class="post-metadata">

**Author:** ![psychometry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/psychometry/32/79114_2.png) [@psychometry](https://forum.freecodecamp.org/u/psychometry)\
**Post date:** [April 29, 2017, 1:36am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/19 "2017-04-29T01:36:45Z")

</div>

I wrote a similar code, but I was only able to calculate up to the hundreds before it give me a null object.

I copied your code to repl.it online coding environment to see if it works as well as you said, but it gave me infinity. I want to know if this is a JavaScript problem or a machine problem that I couldn’t calculate scm(1,5000)

---

<div class="post-metadata">

**Author:** ![forkerino](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/forkerino/32/26793_2.png) [@forkerino](https://forum.freecodecamp.org/u/forkerino)\
**Post date:** [May 8, 2017, 6:18pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075/21 "2017-05-08T18:18:13Z")

</div>

I kinda like this:

```auto
function smallestCommons(arr) {
  const min = Math.min(arr[0],arr[1]), 
        max = Math.max(arr[0],arr[1]),
        range = [...Array(max+1).keys()].slice(min); 

  return range.reduce(function(a,b){
    for (let i = a; i<=a*b; i+=a){
      if (i % b===0 ) return i;
    } 
  });
}

```

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-smallest-common-multiple/16075.md?page=2)
