10000 [ADD] smallest common added · luisprooc/js-algorithms@c6c76a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6c76a4

Browse files
committed
[ADD] smallest common added
1 parent eb4d2b3 commit c6c76a4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,29 @@ dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}) should return [7, 4].
699699

700700
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) should return [3, 9, 2].
701701

702+
```
703+
704+
705+
## Smallest Common Multiple
706+
707+
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.
708+
709+
The range will be an array of two numbers that will not necessarily be in numerical order.
710+
711+
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.
712+
713+
714+
```javascript
715+
smallestCommons([1, 5]) should return a number.
716+
717+
smallestCommons([1, 5]) should return 60.
718+
719+
smallestCommons([5, 1]) should return 60.
720+
721+
smallestCommons([2, 10]) should return 2520.
722+
723+
smallestCommons([1, 13]) should return 360360.
724+
725+
smallestCommons([23, 18]) should return 6056820.
726+
702727
```

src/smallestCommon.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function commonDivisor(arr,divisor){
2+
for(let [small,big] = arr;small <= big; ++small){
3+
if(divisor % small !== 0) return false;
4+
}
5+
return true;
6+
}
7+
8+
function smallestCommons(arr) {
9+
arr = arr.sort((a,b) => a-b);
10+
let [small,big] = arr;
11+
let divisor = big * 2;
12+
while(true){
13+
if(divisor % small === 0 && divisor % big === 0){
14+
if(commonDivisor(arr,divisor)) return divisor;
15+
}
16+
++divisor;
17+
}
18+
19+
}
20+
21+
console.log(smallestCommons([2,10]));

0 commit comments

Comments
 (0)
0