Skip to content

Instantly share code, notes, and snippets.

@aldraco
Created February 26, 2015 20:32
Show Gist options
  • Save aldraco/d6932a417b87605ddc83 to your computer and use it in GitHub Desktop.
Save aldraco/d6932a417b87605ddc83 to your computer and use it in GitHub Desktop.
lowest common divisor - in progress
//Find the smallest number that is evenly divisble by all numbers in the provided range.
function smallestCommons(arr) {
var primeArr =[];
var compArr= [];
for(var i = arr[0]; i <= arr[1]; i++){
// call is prime function on each number and push prime one way and comp another
if(isPrime(i)){
primeArr.push(i);
} else {
compArr.push(i);
}
}
return compArr;
}
function isPrime(n) {
if((n <= 3) &&( n > 0) ){return true;}
var m = Math.sqrt(n);
for(var i=2; i<= m; i++){
if (n % i === 0){return false;}
}
return true;
}
smallestCommons([1,5]);
// 1 = 1 = 11
// 2 = 2 = 21
// 3 = 3 = 31
// 4 = 2 * 2 = 22
// 5 = 5 = 51
// lcm(1, 2, 3, 4, 5) = 1 * 2 * 2 * 3 * 5 = 11 * 22 * 31 * 51 = 60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment