-
Notifications
You must be signed in to change notification settings - Fork 341
Bonfire Smallest Common Multiple
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | My Original Wiki
- 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.
function smallestCommons(arr) {
return arr;
}
smallestCommons([1,5]);
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.
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.
You can use modulo %
to check if the reminder is 0, which means it is evenly divisible.
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.
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]);
- 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.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3