Created
March 14, 2016 04:06
-
-
Save srkama/8ece43e56f47ea1425a4 to your computer and use it in GitHub Desktop.
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. The range will be an array of two numbers that will not necessarily be in numerical order. e.g. for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly…
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) { | |
| if(arr[0]>arr[1]) { | |
| min = arr[1]; | |
| max = arr[0]; | |
| } else { | |
| min = arr[0]; | |
| max = arr[1]; | |
| } | |
| function range(min, max) { | |
| var arr = []; | |
| for (var i = min; i <= max; i++) { | |
| arr.push(i); | |
| } | |
| return arr; | |
| } | |
| function gcd(a, b) { | |
| return !b ? a : gcd(b, a % b); | |
| } | |
| function lcm(a, b) { | |
| return (a * b) / gcd(a, b); | |
| } | |
| var multiple = min; | |
| range(min, max).forEach(function(n) { | |
| multiple = lcm(multiple, n); | |
| }); | |
| return multiple; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment