Created
January 21, 2022 03:28
-
-
Save suissa/8fbcbbff0272db59b8efa4cebc0007e8 to your computer and use it in GitHub Desktop.
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 trampoline(f) { | |
return function trampolined(...args) { | |
let result = f.bind(null, ...args); | |
while (typeof result === 'function') result = result(); | |
return result; | |
}; | |
} | |
function _factorial(n) { | |
if (n <= 1) return 1; | |
return n * _factorial(n - 1); | |
} | |
const factorial = trampoline(function _factorial(n, acc = 1) { | |
if (n <= 1) return acc; | |
return () => _factorial(n - 1, n * acc); | |
}); | |
const produtorio = (start, end) => | |
Array | |
.from({ length: start - end + 1 }, (v, k) => k + end) | |
.reduce((total, n) => total * n , 1) | |
const Comb = (n, k) => | |
factorial(n) / (factorial(k) * factorial(n-k)) | |
const CombSuissa = (n, k) => | |
(k == 2) | |
? (n/k) * (n-1) | |
: produtorio(n, n - k + 1) / factorial(k) | |
const n = 15 | |
const k = 3 | |
console.log( | |
Comb(n, k) | |
) | |
console.log( | |
CombSuissa(n, k) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment