@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
22 lines (18 loc) • 725 B
JavaScript
/**
* Compute factorial of X (X!)
* Note that factorial of 13 is already 6,227,020,800 which exceeds 32bit integer limit
* Note that this implementation is intended for small inputs only
* @example factorial(5) == 5 * 4 * 3 * 2 * 1 == 120
* @param {number} x must be an integer
* @return {number}
*/
export function factorial(x) {
// NOTE: implementation is naive and pretty slow, consider following for improvements:
// * http://www.luschny.de/math/factorial/FastFactorialFunctions.htm
// * https://stackoverflow.com/questions/1751334/fast-algorithms-for-computing-the-factorial
let result = 1;
for (let i = x; i > 0; --i) {
result *= i;
}
return result;
}