# 阶乘

非负整数 n 的阶乘，记作 n!，是所有从 n 到 1 的正整数的乘积，即 n × (n−1) × … × 2 × 1，并约定 0! = 1。<sup>[1](https://xlinux.nist.gov/dads/HTML/factorial.html)</sup> 例如，5! = 120。<sup>[1](https://xlinux.nist.gov/dads/HTML/factorial.html)</sup> 阶乘计数的是将 n 个不同物体排成顺序的方式数，它贯穿于组合数学、概率论以及初等函数的级数展开之中。

| 关键事实 | 细节 |
|---|---|
| 定义 | 对整数 n ≥ 0，n! = n × (n−1) × … × 2 × 1，且 0! = 1<sup>[1](https://xlinux.nist.gov/dads/HTML/factorial.html)</sup> |
| 递推关系 | 当 n > 1 时，n! = n × (n−1)!，不过在代码中一个简单的循环通常更快，也同样清晰<sup>[1](https://xlinux.nist.gov/dads/HTML/factorial.html)</sup> |
| 计数用途 | n 个物体的 r-排列数为 n!/(n−r)!；r-组合数为 n!/(r!(n−r)!)<sup>[2](https://opentext.uleth.ca/Combinatorics/ch_perms_combs.html)</sup> |
| 连续延拓 | n! = Γ(n+1)，其中 Γ 为 gamma 函数，对一切数有定义，而阶乘则严格说来只对非负整数有定义<sup>[3](https://xlinux.nist.gov/dads/HTML/gammaFunction.html)</sup> |
| 记号 | 记号 n! 由克里斯蒂安·克兰普（Christian Kramp）引入；“factorial”一词则由阿尔博加斯特（Arbogast）提出<sup>[4](https://mathshistory.st-andrews.ac.uk//Biographies/Kramp/)</sup> |
| 增长 | 当 n 很大时，(n/e)^n √(2nπ) < n! < (n/e)^n (1 + 1/(12n−1)) √(2nπ)（斯特林公式）<sup>[5](https://xlinux.nist.gov/dads/HTML/stirlingsFormula.html)</sup> |
| 快速计算 | 借助快速乘法，n! 可在 O(n(log n log log n)²) 位复杂度内算出<sup>[6](https://www.sciencedirect.com/science/article/pii/0196677485900069)</sup> |

## 定义与基本性质

阶乘对每个非负整数都有定义。0! = 1 这一取值可由 gamma 函数的定义推出：0! = Γ(0+1) = ∫₀^∞ e^(−x)x^(1−1) dx = ∫₀^∞ e^(−x) dx = 1。<sup>[1](https://xlinux.nist.gov/dads/HTML/factorial.html)</sup> 令 0! = 1 也使计数公式得以成立：将 n 个物体全部排列，方式数为 n!/(n−n)! = n!/0! = n!。<sup>[7](https://openstax.org/books/contemporary-mathematics/pages/7-2-permutations)</sup>

阶乘满足递推关系 n! = n × (n−1)!（n > 1）。算法教科书常把它用作递归的例子，但一个简单的循环通常更快，也同样清晰。<sup>[1](https://xlinux.nist.gov/dads/HTML/factorial.html)</sup> 迭代实现与递归实现的增长阶相同。<sup>[8](https://iitd.github.io/col100/lec/efficiency2.html)</sup>

## 在计数中的作用

阶乘是排列数与组合数计数的基础。n 个物体的 r-排列数，即从 n 个中有序选取 r 项的方式数，为 n!/(n−r)!。r-组合数，即无序选取的方式数，为二项式系数 \( \binom{n}{r} = \frac{n!}{r!(n-r)!} \)。<sup>[2](https://opentext.uleth.ca/Combinatorics/ch_perms_combs.html)</sup> 二项式定理用这些量来表达 (a+b)^n 的系数。<sup>[2](https://opentext.uleth.ca/Combinatorics/ch_perms_combs.html)</sup>

阶乘也直接出现在概率公式中。泊松分布刻画给定时间区间内发生的事件数，其概率质量函数为 \( p(x;\lambda) = \frac{e^{-\lambda}\lambda^{x}}{x!} \)，其中 x = 0, 1, 2, …，λ 为该区间内事件的平均发生次数。<sup>[9](https://www.itl.nist.gov/div898/handbook/eda/section3/eda366j.htm)</sup> 在微积分中，函数的泰勒级数将第 n 阶导数项除以 n 的阶乘。<sup>[10](https://mitocw.ups.edu.ec/courses/mathematics/18-01-single-variable-calculus-fall-2006/video-lectures/lecture-38-taylors-series/)</sup>

## 向非整数变元的延拓

非整数的阶乘不能由乘积公式定义，但 gamma 函数提供了标准的延拓。n 的 gamma 函数为 Γ(n) = ∫₀^∞ e^(−x)x^(n−1) dx，且对非负整数有 Γ(n+1) = n!。<sup>[3](https://xlinux.nist.gov/dads/HTML/gammaFunction.html)</sup> gamma 函数对复数 z 满足递推关系 Γ(z+1) = zΓ(z)，这与阶乘所服从的函数方程相同。<sup>[11](https://dlmf.nist.gov/5.5)</sup> gamma 函数对一切数有定义，而阶乘则严格说来只对非负整数有定义。<sup>[3](https://xlinux.nist.gov/dads/HTML/gammaFunction.html)</sup>

一个凸性条件唯一地确定了这一延拓：玻尔–莫勒鲁普定理（Bohr–Mollerup theorem）指出，若 (0, ∞) 上的正函数 f(x) 满足 f(x+1) = x f(x)、f(1) = 1，且 ln f(x) 为凸函数，则 f(x) = Γ(x)。<sup>[11](https://dlmf.nist.gov/5.5)</sup> 软件同时反映了该函数的这两种视角：SciPy 的阶乘例程在 exact=False 时用 gamma 函数近似取值，即 n! = Γ(n+1)，这也正是其复数延拓的定义；而在 exact=True 时则用整数算术算出结果。<sup>[12](https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html)</sup>

## 记号的历史

用 n! 表示从 n 递减到 1 的各数之积，这一记号出自法国数学家克里斯蒂安·克兰普（Christian Kramp，1760–1826）之手，他是一位数学教授，曾任斯特拉斯堡理学院院长，研究分析与组合数学。<sup>[4](https://mathshistory.st-andrews.ac.uk//Biographies/Kramp/)</sup> 在 1798 年前后关于此题的早期工作中，他把这些乘积称为“faculty”（facultés analytiques，分析阶乘），但在其《万有算术要义》（Éléments d'arithmétique universelle，1808）的序言中，他采用了同乡阿尔博加斯特（Arbogast）提出的“factorial”一名，并写道，他很高兴以此向亡友致意。<sup>[4](https://mathshistory.st-andrews.ac.uk//Biographies/Kramp/)</sup> 在同一部著作中，他解释说，自己在组合分析中不断使用这一概念，使得简洁的记号 n! 成为必要。<sup>[4](https://mathshistory.st-andrews.ac.uk//Biographies/Kramp/)</sup>

克兰普与贝塞尔（Bessel）、勒让德（Legendre）、高斯（Gauss）一样，将阶乘的概念推广到非整数变元，并于 1812 年发表了一份这些数值的数值表，寄给了贝塞尔。<sup>[13](https://mathshistory.st-andrews.ac.uk/DSB/Kramp.pdf)</sup> 这一函数更深层的历史属于詹姆斯·斯特林（James Stirling），他于 1730 年发现了阶乘的著名渐近公式；也属于莱昂哈德·欧拉（Leonhard Euler），他引入 gamma 函数作为阶乘在非整数变元上的自然延拓。<sup>[14](https://functions.wolfram.com/GammaBetaErf/Factorial/introductions/FactorialBinomials/01/)</sup>

## 增长与近似

阶乘的增长快于任何固定的幂，也快于几何序列，这一事实例如被用于证明指数函数泰勒级数的收敛性。<sup>[15](https://math.fel.cvut.cz/mt/txte/3/txe4ea3b.htm)</sup> 斯特林公式使这一增长变得精确。当 n 很大时，<sup>[5](https://xlinux.nist.gov/dads/HTML/stirlingsFormula.html)</sup>

\[ \left(\frac{n}{e}\right)^{n} \sqrt{2n\pi} < n! < \left(\frac{n}{e}\right)^{n} \left(1 + \frac{1}{12n-1}\right) \sqrt{2n\pi}. \]

DLMF 将 gamma 函数相应的展开称为斯特林级数，其首项即称为斯特林公式；当该展开对实的正 z 在 n 项后截断时，余项的绝对值以第一个被舍弃的项为界，且符号与之相同。<sup>[16](https://dlmf.nist.gov/5.11)</sup> 用 gamma 函数表示，一般的渐近形式为：对固定的 a > 0，Γ(az+b) ∼ √(2π) e^(−az) (az)^(az+b−1/2)。<sup>[16](https://dlmf.nist.gov/5.11)</sup>

## 计算

对于精确的整数值，朴素的循环或递归对小 n 足够，但对大 n 则很浪费。真正严肃的算法利用 n! 的素因数分解：《算法杂志》（Journal of Algorithms）上的一项分析表明，结合快速乘法，这可以得到一个计算 n! 的 O(n(log n log log n)²) 复杂度算法。<sup>[6](https://www.sciencedirect.com/science/article/pii/0196677485900069)</sup> 素数摆动法（prime swing method）在假定快速乘法的前提下把问题的位复杂度降到大约 O(n log n)，至今仍属已知最高效的阶乘算法之列。<sup>[17](https://hvks.com/Numerical/Downloads/HVE%20Fast%20Factorial%20in%20arbitrary%20precision.pdf)</sup> 彼得·卢施尼（Peter Luschny）维护着素数摆动算法在 SageMath、Python、Julia、Java、C#、Go 和 C++ 中的实现。<sup>[18](https://github.com/PeterLuschny/Fast-Factorial-Functions)</sup>

主流软件采用了这些思路。Python 的 math.factorial(n) 以整数返回 n 的阶乘，若 n 不是整数或为负数则抛出 ValueError。<sup>[19](https://docs.python.org/3.11/library/math.html)</sup> 对于近似值，各库则退回到 gamma 函数，正如 SciPy 在 exact=False 时所做的那样。<sup>[12](https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html)</sup>

浮点存储限制了精确序列所能表示的范围：在 32 位无符号整数下，按递归方案中每次函数调用约 16 字节的常见估计，计算 13! 就已溢出。<sup>[20](https://planetmath.org/recursivealgorithmforfactorialfunction)</sup>

## 推广

阶乘有一族亲属。波赫哈默尔符号（Pochhammer symbol），也称移位阶乘，为 (a)_n = a(a+1)(a+2)⋯(a+n−1) = Γ(a+n)/Γ(a)。<sup>[21](https://dlmf.nist.gov/5.2)</sup> 双阶乘 n!! 可用 gamma 函数表示，偶数 n 与奇数 n 的因子不同。<sup>[22](https://dlmf.nist.gov/5.4)</sup> 在 q-模拟中，q-阶乘定义为 n!_q = 1(1+q)⋯(1+q+⋯+q^(n−1)) = (q;q)_n (1−q)^(−n)，其中 (q;q)_n 为 q-波赫哈默尔符号。<sup>[23](https://dlmf.nist.gov/5.18)</sup> 多重 gamma 函数是欧拉 gamma 函数由递推函数方程定义的推广，由金克林（Kinkelin）、格莱舍（Glaisher）和巴恩斯（Barnes）于 1900 年前后引入。<sup>[24](https://arxiv.org/abs/math/0308074)</sup>

## References

1. [Paul E. Black, "factorial", NIST Dictionary of Algorithms and Data Structures](https://xlinux.nist.gov/dads/HTML/factorial.html)
2. [Permutations, Combinations, and the Binomial Theorem, University of Lethbridge open text](https://opentext.uleth.ca/Combinatorics/ch_perms_combs.html)
3. [Paul E. Black, "gamma function", NIST Dictionary of Algorithms and Data Structures](https://xlinux.nist.gov/dads/HTML/gammaFunction.html)
4. [Christian Kramp (1760–1826), MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk//Biographies/Kramp/)
5. [Paul E. Black, "Stirling's formula", NIST Dictionary of Algorithms and Data Structures](https://xlinux.nist.gov/dads/HTML/stirlingsFormula.html)
6. [On the complexity of calculating factorials, Journal of Algorithms](https://www.sciencedirect.com/science/article/pii/0196677485900069)
7. [7.2 Permutations, Contemporary Mathematics, OpenStax](https://openstax.org/books/contemporary-mathematics/pages/7-2-permutations)
8. [Efficiency of recursive implementations, COL100 lecture notes, IIT Delhi](https://iitd.github.io/col100/lec/efficiency2.html)
9. [1.3.6.6.19. Poisson Distribution, NIST/SEMATECH e-Handbook of Statistical Methods](https://www.itl.nist.gov/div898/handbook/eda/section3/eda366j.htm)
10. [Lecture 38: Taylor's Series, MIT OpenCourseWare 18.01](https://mitocw.ups.edu.ec/courses/mathematics/18-01-single-variable-calculus-fall-2006/video-lectures/lecture-38-taylors-series/)
11. [DLMF §5.5 Functional Relations, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/5.5)
12. [scipy.special.factorial, SciPy v1.18.0 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html)
13. [Kramp, Chrétien or Christian, Complete Dictionary of Scientific Biography](https://mathshistory.st-andrews.ac.uk/DSB/Kramp.pdf)
14. [Factorial: Introduction to the factorials and binomials, Wolfram Functions](https://functions.wolfram.com/GammaBetaErf/Factorial/introductions/FactorialBinomials/01/)
15. [Math Tutor – Series: function series, Czech Technical University](https://math.fel.cvut.cz/mt/txte/3/txe4ea3b.htm)
16. [DLMF §5.11 Asymptotic Expansions, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/5.11)
17. [Fast Factorial & Binomial Computation for Arbitrary Precision](https://hvks.com/Numerical/Downloads/HVE%20Fast%20Factorial%20in%20arbitrary%20precision.pdf)
18. [PeterLuschny/Fast-Factorial-Functions, GitHub](https://github.com/PeterLuschny/Fast-Factorial-Functions)
19. [math — Mathematical functions, Python 3.11 documentation](https://docs.python.org/3.11/library/math.html)
20. [Recursive algorithm for factorial function, PlanetMath](https://planetmath.org/recursivealgorithmforfactorialfunction)
21. [DLMF §5.2 Definitions, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/5.2)
22. [DLMF §5.4 Special Values and Extrema, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/5.4)
23. [DLMF §5.18 q-Gamma and q-Beta Functions, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/5.18)
24. [Multiple Gamma Function and Its Application to Computation of Series, arXiv:math/0308074](https://arxiv.org/abs/math/0308074)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Combinatorics › Enumerative combinatorics › Counting techniques and recurrences › Recurrence relations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/factorial

English original: https://www.edgepedia.org/factorial
