Created
November 22, 2015 05:19
-
-
Save minsooshin/c5158dc3abf485ac8d5a to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Smallest Common Multiple
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
// 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