Last active
April 28, 2017 03:07
-
-
Save the-vampiire/8c023139ca9e678ed8eb5fcd14d32552 to your computer and use it in GitHub Desktop.
Lowest 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
| function smallestCommons(arr) { | |
| // sort to ensure smallest to largest | |
| function sorting_hat(a,b){ | |
| return a-b; | |
| } | |
| arr.sort(sorting_hat); | |
| // set lower and upper bounds for range | |
| var low = arr[0], high = arr[1], range = []; | |
| for(low; low <= high; low++){ | |
| range.push(low); | |
| } | |
| var rev_range = range.reverse(); | |
| var a = rev_range[0], | |
| b = rev_range[1], | |
| sum = 0, | |
| multiples = [], | |
| LCM = []; | |
| while(true){ | |
| sum += (a*b); | |
| multiples.push(sum); | |
| if(multiples.length === 100000){ | |
| break; | |
| } | |
| } | |
| for(var i = 0; i < multiples.length; i++){ | |
| if(range.every(divides)){ | |
| LCM.push(multiples[i]); | |
| } | |
| } | |
| function divides(a){ | |
| return multiples[i] % a === 0; | |
| } | |
| return LCM.shift(); | |
| } | |
| console.log(smallestCommons([1,5])); | |
| // console.log(tits([18,23])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment