-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0254-factor-combinations.js
More file actions
27 lines (23 loc) · 903 Bytes
/
0254-factor-combinations.js
File metadata and controls
27 lines (23 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Factor Combinations
* Time Complexity: O(C * logN)
* Space Complexity: O(logN)
*/
var getFactors = function (n) {
const finalResultCollection = [];
const initiateFactorSearch = (currentValue, minimumPossibleFactor, currentFactorSequence) => {
for (let loopFactor = minimumPossibleFactor; loopFactor * loopFactor <= currentValue; loopFactor++) {
if (currentValue % loopFactor === 0) {
const quotientValue = currentValue / loopFactor;
if (quotientValue >= loopFactor) {
finalResultCollection.push([...currentFactorSequence, loopFactor, quotientValue]);
initiateFactorSearch(quotientValue, loopFactor, [...currentFactorSequence, loopFactor]);
}
}
}
};
if (n >= 2) {
initiateFactorSearch(n, 2, []);
}
return finalResultCollection;
};