Created
September 15, 2015 14:30
-
-
Save weargoggles/f886b4ce204d6dda0c04 to your computer and use it in GitHub Desktop.
lowest common multiple of numbers less than another number
This file contains 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 gcd(a, b) { | |
var a_ = a; | |
var b_ = b; | |
while (a_ != 0 && b_ != 0) { | |
if (a_ > b_) { | |
a_ = a_ % b_; | |
} else { | |
b_ = b_ % a_; | |
} | |
} | |
if (a_ == 0) { | |
return b_; | |
} else { | |
return a_; | |
} | |
} | |
function lcm(a, b) { | |
return a / gcd(a, b) * b; | |
} | |
function lcm_rec(k) { | |
if (k == 1) { | |
return 1; | |
} else { | |
return lcm(k, lcm_rec(k-1)); | |
} | |
} | |
var then = performance.now(); | |
var n = 20; | |
console.log("lowest common multiple of numbers <= ", n, lcm_rec(n)) | |
console.log("that took: ", performance.now() - then, " milliseconds"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment