Skip to content

Instantly share code, notes, and snippets.

@minsooshin
Created November 22, 2015 05:19
Show Gist options
  • Save minsooshin/c5158dc3abf485ac8d5a to your computer and use it in GitHub Desktop.
Save minsooshin/c5158dc3abf485ac8d5a to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Smallest Common Multiple
// Bonfire: Smallest Common Multiple
// Author: @minsooshin
// Challenge: http://www.freecodecamp.com/challenges/bonfire-smallest-common-multiple
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function smallestCommons(arr) {
arr.sort();
var nums = [], multiple = arr[0];
for (var i = arr[0]; i <= arr[1]; i++) {
nums.push(i);
}
function gcd(a, b) {
return !b ? a : gcd(b, a % b);
}
function scm(a, b) {
return a * b / gcd(a, b);
}
return nums.reduce(function(mul, cur) {
return scm(mul, cur);
});
}
smallestCommons([1,5]);
smallestCommons([5,1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment