factorial function : product of the n first positive integers
f = factorial(n) [f, p] = factorial(n) [f, p, m] = factorial(n)
scalar, vector, matrix or hypermatrix of positive integers <= 1014.
array of doubles, of the sizes of n: f(i) = n(i)!
.
f
values are exact up to n=21
included.
array of doubles, of the sizes of n: power of 10 of f
:
p(i) = int(log10(f(i)!))
.
array of doubles in [1,10[, of the sizes of n: Mantissae of f
,
such that n(i)! = m(i) * 10^p(i)
.
Returns the factorial of n, that is the product 1 * 2 * ... * n.
![]() | f overflows as soon as n>170 and always returns %inf for any
bigger n. |
![]() |
|
Table of the first n! exact values :
--> [n factorial(n)] ans = 0. 1. 1. 1. 2. 2. 3. 6. 4. 24. 5. 120. 6. 720. 7. 5040. 8. 40320. 9. 362880. 10. 3628800. 11. 39916800. 12. 479001600. 13. 6227020800. 14. 87178291200. 15. 1307674368000. 16. 20922789888000. 17. 355687428096000. 18. 6402373705728000. 19. 121645100408832000. 20. 2432902008176640000. 21. 51090942171709440000.
Ceiling of factorial() in floating point representation:
Plot the function on its whole range:
x = (10^(0:13)).*.(1:9); x(1)=[]; x($)=1e14; [f, p, m] = factorial(x); clf plot2d("ll", x, p+log10(m)) xlabel("n", "fontsize",3) title("$\mathsf{log_{10}(n!)}$", "fontsize", 4) xgrid(color("grey70"), 1, 7) set(gca(), "sub_ticks",[8 2], "tight_limits","on"); gca().data_bounds([1 4]) = [1 2e15]; gcf().axes_size = [850 480]; | ![]() | ![]() |
Relative factorial() errors:
--> [n r_err] ans = 10. 0. 100. -5.551D-16 1000. 1.132D-13 10000. 1.918D-12 100000. 6.611D-12 1000000. 9.962D-11 10000000. 5.048D-08 100000000. 1.050D-08 1.000D+09 0.0000001 1.000D+10 0.0000019 1.000D+11 0.0000062 1.000D+12 0.0001327 1.000D+13 0.0004839 1.000D+14 0.0071116
Version | Description |
6.1 | Extension up to n = 1014. p
10-power and m mantissa output added. |