/**
 * 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;
}