Factorial function equals to the product of sequence of natural numbers from 1 to size n:
$$ n! = n \times (n-1) \times (n-2) \times \dotsb \times 3 \times 2 \times 1 = n \times (n-1)! $$
This function computes the factorial number of specified natural number by using very efficient
implementation. Implementation, however, omits use of memoization in favor of pure function call.
From the perspective of IEEE 754 specification, maximal possible factorial number countable in JavaScript
is for \( n=170! \), while for any number larger than that (i.e. $ n \geq 171 $), JS engine counts Inifinity.
Therefore, factorial function returns Infinity value immediately for any such number. Likewise, for any
negative integer or any non-natural number immediately returns NaN value.
Function returns factorial of passed number value
Remarks
Factorial function equals to the product of sequence of natural numbers from 1 to size n: $$ n! = n \times (n-1) \times (n-2) \times \dotsb \times 3 \times 2 \times 1 = n \times (n-1)! $$
This function computes the factorial number of specified natural number by using very efficient implementation. Implementation, however, omits use of memoization in favor of pure function call. From the perspective of IEEE 754 specification, maximal possible factorial number countable in JavaScript is for \( n=170! \), while for any number larger than that (i.e. $ n \geq 171 $), JS engine counts Inifinity. Therefore,
factorial
function returnsInfinity
value immediately for any such number. Likewise, for any negative integer or any non-natural number immediately returnsNaN
value.Example
Simple use of finding triangular number: