Last active
April 17, 2016 08:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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