How to use the factorial function from mathjs

Find comprehensive JavaScript mathjs.factorial code examples handpicked from public code repositorys.

593
594
595
596
597
598
599
600
601
602
603
604




/** Calculates how many seconds it will take for the algorithm to finish multiplied
 *  by 10. Multiplied by 10 to prevent rounding errors. */
function calculateRunningTime(cities) {
  var factorialTemp = Mathjs.factorial(cities - 1);
  var numPaths = Mathjs.divide(factorialTemp, 2);
  // The number of seconds is numPaths / 10 but we are now multiplying by 10 to prevent
  // rounding errors so return numPaths. This will be divided by 10 in calculateTimeUnits() when we
  // divide seconds by secondsInUnitOfTime (both are multiplied by 10).
fork icon75
star icon325
watch icon0

186
187
188
189
190
191
192
193
194
195
if (operator === 'beta') {
    if (isConst(args[0])) {args = [args[1], args[0]];}
    if (isConst(args[1])) {
        const val = getConst(args[1]);
        if (val.im === 0 && Number.isInteger(val.re) && val.re > 0) {
            const prefactor = ['component_mul', args[0], math.factorial(val.re)/val.re];
            const terms = [prefactor];
            for (let i = 1; i < val.re; i++) {
                terms.push(['add', args[0], ['number', i, 0]]);
            }
fork icon8
star icon28
watch icon3

+ 44 other calls in file

215
216
217
218
219
220
221
222
223
224
if (operator === 'beta') {
    if (isConst(args[0])) {args = [args[1], args[0]];}
    if (isConst(args[1])) {
        const val = getConst(args[1]);
        if (val.im === 0 && Number.isInteger(val.re) && val.re > 0 && val.re < 20) {
            const prefactor = compile(['component_mul', args[0], math.factorial(val.re)/val.re]);
            const terms = [prefactor];
            for (let i = 1; i < val.re; i++) {
                terms.push(['add', args[0], ['number', i, 0]]);
            }
fork icon8
star icon28
watch icon3

+ 110 other calls in file

72
73
74
75
76
77
78
79
80
math.sum(numbers);

// probability
math.combinations(7, 5) // number of ways picking 'k' unordered outcomes from 'n' possibilities
math.combinationsWithRep(7, 5) // combinations with replacement
math.factorial(a);
math.permutations(n) // number of possible ordered combinations
math.pickRandom(numbers) // random selection from array
math.randomInt(0, 11) // random int (min <= x < max)
fork icon0
star icon3
watch icon0

+ 6 other calls in file