Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaniGithub/83b583e4eb348d72c61c7b60ac9e7430 to your computer and use it in GitHub Desktop.
Save DaniGithub/83b583e4eb348d72c61c7b60ac9e7430 to your computer and use it in GitHub Desktop.
Bonfire Smallest Common Multiple https://github.com/freecodecamp/freecodecamp/wiki/Algorithm-Smallest-Common-Multiple Not efficient at all
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]);
// noprotect
// Not efficient at all
function smallestCommons(arr) {
arr = arr.sort();
arr[0] = 2, newArr = [];
//Array setup;
for(var i = 2; i <= arr[arr.length-1]; i++) {
newArr[i-2] = [i];
}
//Add multiples to arrays
function addToArrays(a, max) {
var d = [];
for(var i = 0; i < a.length; i++) {
for(var j = 0; j <= max; j++) {
a[i].push(a[i][j] + a[i][0]);
}
d = d.concat(a[i]).sort(function(a, b) {
return a - b;
});
}
return d;
}
var numArray = addToArrays(newArr,200000);
var getRange = -3 + arr[arr.length-1];
numArray = numArray.find(function(v,i,a) {
if(v === a[i-1] && v === a[i+getRange]) {
return v;
}
});
return numArray || "Not-Found";
}
console.log(smallestCommons([1, 13]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment