Skip to content

Bonfire Smallest Common Multiple

Rafael J. Rodriguez edited this page Jan 6, 2017 · 2 revisions

Author

@Rafase282

Created by Rafase282

Github | FreeCodeCamp | CodePen | LinkedIn | Website | My Original Wiki

Details

  • Difficulty: 2/5

Find the smallest number that is evenly divisible by all numbers in the provided range.

The range will be an array of two numbers that will not necessarily be in numerical order.

Remember to use RSAP if you get stuck. Try to pair program. Write your own code.

Useful Links

Problem Script:

function smallestCommons(arr) {
  return arr;
}


smallestCommons([1,5]);

Explanation:

This particular problem can be confusing because most people look for the smallest common multiple of the two number but forget the keyword range. This means that if you get [1,5] then you have to check for the smallest common multiple for all these numbers [1,2,3,4,5] that is evenly divisible by all of them.

Hint: 1

Create an array with all the numbers that are missing from the original array to make it easier to check when having to check for even division.

Hint: 2

You can use modulo % to check if the reminder is 0, which means it is evenly divisible.

Hint: 3

If you sort the array from greater to lowest then you can check for the first two numbers as it is more likely to the the smallest common multiple than the lower numbers.

My code:

function smallestCommons(arr) {
  // Sort array from greater to lowest
  // This line of code was from Adam Doyle (http://github.com/Adoyle2014)
  arr.sort(function(a, b) {
    return b - a;
  });

  // Create new array and add all values from greater to smaller from the original array.
  var newArr = [];
  for (var i = arr[0]; i >= arr[1]; i--) {
    newArr.push(i);
  }

  // Variables needed declared outside the loops.
  var quot = 0;
  var loop = 1;
  var n;

  // run code while n is not the same as the array lenght.
  do {
    quot = newArr[0] * loop * newArr[1];
    for (n = 2; n < newArr.length; n++) {
      if (quot % newArr[n] !== 0) {
        break;
      }
    }

    loop++;
  } while (n !== newArr.length);

  return quot;
}

smallestCommons([1, 13]);

My Code Explanation:

  • Because the possibility of the smallest common denominator being among the two biggest numbers, it makes sense to check those first, so sort the array.
  • Create a new array to sore all the numbers.
  • Use a descending for loop to add the numbers from the biggest to the smallest in the new array.
  • Declare the variables for the quotient, the number of loops and the variable that we will use in a for loop on another scope, this will allow us access outside the loop.
  • Use a do while loop to check what we need while n is not the same length as the new array.
  • In the do part, we are going to multiply the very first number, times the number of loops, times the second number.
  • The loop part will allows us to increase the number beyond the greatest number we have without having to change the algorithm.
  • We enter a for loop that will go from n being 2 and going up by one while it is smaller than the array with all the numbers.
  • If the quotient is not even then stop the loop. If it is even then it check for the next elements in the array until it is not even or we find our answer.
  • Outside the loop, increase the value of loop.
  • At the end of the loop return the quotient.

If the array only has two elements then the for loop never gets used and the return value is the product of said numbers. Otherwise, from the third element and until n is the same and the array length check if the reminder of the quotient and the third value of the array is not 0, if it is not 0 then stop loop increases and then we start over. If the reminded was 0 then keep checking until the end of the array.

Getting Started

  1. Welcome!
  2. Contact
  3. Get Started with Free Code Camp

Front End Development Certification

  1. HTML5 and CSS
  2. Responsive Design with Bootstrap
  3. Gear up for Success
  4. jQuery
  5. Basic JavaScript
  6. Object Oriented and Functional Programming
  7. Basic Algorithm Scripting
  8. Basic Front End Development Projects
  9. Intermediate Algorithm Scripting
  10. JSON APIs and Ajax
  11. Intermediate Front End Development Projects
  12. Claim Your Front End Development Certificate

Data Visualization Certification

  1. SASS
  2. React
  3. React Projects
  4. D3
  5. Data Visualization Projects
  6. Claim Your Data Visualization Certificate

Back End Development Certification

  1. Upper Intermediate Algorithm Scripting
  2. Automated Testing and Debugging
  3. Advanced Algorithm Scripting
  4. AngularJS (Legacy Material)
  5. Git
  6. Node.js and Express.js
  7. MongoDB
  8. API Projects
  9. Dynamic Web Applications
  10. Claim Your Back End Development Certificate

Full Stack Development Certification

  1. Greefield Nonprofit Project 1
  2. Greefield Nonprofit Project 2
  3. Legacy Nonprofit Project 1
  4. Legacy Nonprofit Project 2
  5. Claim your Full Stack Development Certification

Coding Interview Preparation

  1. Whiteboard Coding Interview Training
  2. Critical Thinking Interview Training
  3. Mock Interview 1
  4. Mock Interview 2
  5. Mock Interview 3
Clone this wiki locally