# 素数公式

素数公式是指用一个或多个变量构成的数学表达式，它能产生素数——或只取素数值，或取遍且仅取所有素数值，或以n为自变量返回第n个素数。这类公式确实存在，且已为人所知数十年，但每一个已知例子要么预先将素数编码其中，要么要求以极高精度知道某个未知常数，要么在计算上远比直接运行筛法（通过系统划去合数来寻找素数的方法）慢得多。<sup>[1](https://mathworld.wolfram.com/PrimeFormulas.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 米尔斯公式 | 存在实数常数 \( A > 1 \)，使得对每个正整数n，\( \lfloor A^{3^{n}} \rfloor \) 均为素数（米尔斯，1947）<sup>[2](https://arxiv.org/abs/2004.01285)</sup> |
| 米尔斯常数 | 在黎曼假设下，最小的这种A约为1.3063778838……；2025年其无理性得到证明<sup>[3](https://arxiv.org/abs/2404.19461)</sup><sup> • </sup><sup>[4](https://londmathsoc.onlinelibrary.wiley.com/doi/full/10.1112/mtk.70027)</sup> |
| 威兰斯公式 | 利用威尔逊定理给出的第n个素数的精确表达式，1964年发表，但需要 \( 2^{n} \) 个求和项<sup>[5](https://www.cambridge.org/core/journals/mathematical-gazette/article/abs/on-formulae-for-the-nth-prime-number/43E49D11DFEAD3E4CBC12F17C87F5EE1)</sup><sup> • </sup><sup>[1](https://mathworld.wolfram.com/PrimeFormulas.html)</sup> |
| 素数生成多项式 | 琼斯、佐藤、和田与温斯（1976）给出了一个含26个变量、次数为25的多项式，其正值恰好为全体素数<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup> |
| 单变量多项式 | 任何非常数的整系数单变量多项式都不可能只取素数值<sup>[7](https://t5k.org/glossary/page.php?sort=FormulasForPrimes)</sup> |
| 实际状况 | 素数实际上是靠筛法和素性检验算出来的，而非靠公式；AKS检验是首个确定性多项式时间素性检验<sup>[8](https://dlmf.nist.gov/27.18)</sup> |

## 什么算作公式

关于素数公式的讨论通常区分三类对象。<sup>[7](https://t5k.org/glossary/page.php?sort=FormulasForPrimes)</sup> 一个函数可以取遍所有素数值且只取素数值；这类函数构造起来出奇地容易，但通常要先知道素数或对素数计数才能造出来，因此对发现新素数毫无价值。一个函数可以只取素数值而不覆盖全部素数；费马曾误以为费马数 \( 2^{2^{n}} + 1 \) 全属此类，后来欧拉证明 \( 2^{32} + 1 = 4294967297 \) 能被641整除。<sup>[7](https://t5k.org/glossary/page.php?sort=FormulasForPrimes)</sup><sup> • </sup><sup>[9](https://mathshistory.st-andrews.ac.uk/HistTopics/Prime_numbers/)</sup> 最后，一个函数可以取无穷多个素数值而不必只取素数；\( f(n) = n \) 是平凡的例子，狄利克雷定理提供了其他例子，但就连“\( n^{2} + 1 \) 无穷多次取素数值”这一命题至今仍未获证明。<sup>[7](https://t5k.org/glossary/page.php?sort=FormulasForPrimes)</sup>

一个基本的否定性结果框定了整个主题：任何非常数的整系数单变量多项式都不可能只取素数值。<sup>[7](https://t5k.org/glossary/page.php?sort=FormulasForPrimes)</sup> 在多变量的情形下情况有所改变，1976年的多项式即为明证。

## 基于常数的公式：米尔斯与赖特

**米尔斯定理。** 1947年，W. H. 米尔斯（W. H. Mills）证明存在实数常数 \( A > 1 \)，使得对每个正整数n，\( \lfloor A^{3^{n}} \rfloor \) 均为素数。<sup>[2](https://arxiv.org/abs/2004.01285)</sup> 证明依赖于霍海塞尔（1930）与英厄姆（1937）的素数间隙定理，该定理保证存在常数K，使得对所有n都有 \( p_{n+1} - p_{n} < K \, p_{n}^{5/8} \)；这一间隙足够小，使构造的每一步都能落在一个新的素数上。<sup>[10](https://mathworld.wolfram.com/MillsTheorem.html)</sup>

最小的这种A称为米尔斯常数。在黎曼假设下，考德威尔与郑计算出了其十进制展开的6850位数字，开头为 \( 1.3063778838\ldots \)；若不作此假设，则不知道任何具体数值。<sup>[3](https://arxiv.org/abs/2404.19461)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2004.01285)</sup> 该常数是否为有理数，七十多年来悬而未决：2025年，日本大学的斋藤康太（Kota Saito）证明了米尔斯常数是无理数，并取得了关于其超越性的部分结果。<sup>[4](https://londmathsoc.onlinelibrary.wiley.com/doi/full/10.1112/mtk.70027)</sup>

问题既在理论上也在实践上。除非已知 \( A \) 的精确值，否则必须先知道素数本身才能确定它；哈代与赖特以及里本博伊姆都指出，这类公式尽管优美，却没有任何实际后果。<sup>[10](https://mathworld.wolfram.com/MillsTheorem.html)</sup> 在米尔斯与赖特的相关公式中，\( n = 4 \) 时的值增长得如此之快，以至于需要常数极其精确的值才能得到正确的素数，而 \( n \geq 5 \) 时的值实际上无法计算。<sup>[1](https://mathworld.wolfram.com/PrimeFormulas.html)</sup>

**赖特公式。** 1951年，曾在阿伯丁大学工作的E. M. 赖特（E. M. Wright）确立了常数 \( c \) 的存在性，其性质是：定义 \( g_{0} = c \) 及 \( g_{n+1} = 2^{g_{n}} \)，则对每个 \( n > 0 \)，\( \lfloor g_{n} \rfloor \) 均为素数。<sup>[11](https://arxiv.org/abs/1705.09741)</sup><sup> • </sup><sup>[12](https://www.cambridge.org/core/journals/proceedings-of-the-royal-society-of-edinburgh-section-a-mathematics/article/abs/xviiithe-elementary-proof-of-the-prime-number-theorem/E63AA8DF602BA83EF1BB65E62369FFBB)</sup> 这同样是从一个隐藏的常数产生素数，而非来自任何可用的程序。

**无条件变体。** 格拉茨工业大学的数论学家克里斯蒂安·埃尔斯霍尔茨（Christian Elsholtz）于2020年给出了首批米尔斯式的无条件素数表示变体：\( \lfloor A^{10^{10n}} \rfloor \) 为素数，其中 \( A = 1.00536773279814724017\ldots \) 可计算到数百万位；类似地，\( \lfloor A^{3^{13n}} \rfloor \) 为素数，其中 \( A = 3.8249998073439146171615551375\ldots \)。<sup>[2](https://arxiv.org/abs/2004.01285)</sup>

## 威兰斯的第n个素数公式

1964年，伯明翰大学的C. P. 威兰斯（C. P. Willans）在《数学公报》上发表了第n个素数的闭式表达式。<sup>[5](https://www.cambridge.org/core/journals/mathematical-gazette/article/abs/on-formulae-for-the-nth-prime-number/43E49D11DFEAD3E4CBC12F17C87F5EE1)</sup> 它建立在一个由威尔逊定理导出的素数指示函数之上；威尔逊定理指出，\( (j-1)! \equiv -1 \pmod{j} \) 当且仅当j为素数（或j = 1）。定义

\[ F(j) = \left\lfloor \cos^{2}\!\left( \frac{\pi\,((j-1)! + 1)}{j} \right) \right\rfloor, \]

当j为1或素数时它等于1，否则为0。<sup>[13](https://mathworld.wolfram.com/WillansFormula.html)</sup> 对F求和得到素数计数函数 \( \pi(x) = -1 + \sum_{k=1}^{x} F(k) \)，而第n个素数为

\[ p_{n} = 1 + \sum_{m=1}^{2^{n}} \left\lfloor \left( \frac{n}{1 + \pi(m)} \right)^{1/n} \right\rfloor. \]<sup>[13](https://mathworld.wolfram.com/WillansFormula.html)</sup>

该公式对每个n都精确成立，但求和项数为 \( 2^{n} \)，因此对研究素数并不实用；高效筛法求素数要快得多。<sup>[1](https://mathworld.wolfram.com/PrimeFormulas.html)</sup> 实际实现也证实了其实际极限：一份Python实现指出其复杂度约为 \( O(2^{n} \cdot n!) \)，使得该公式在 \( n = 15 \) 左右便无法使用。<sup>[14](https://github.com/valorisa/willans-prime-formula)</sup> 评论者将这一构造描述为用取整函数、阶乘和三角学的语言重写的筛法算法——精确，但在计算上荒谬。<sup>[15](https://shreevatsa.net/post/willans-prime-formula/)</sup>

## 素数生成多项式

最强的存在性结果来自数理逻辑。马丁·戴维斯、尤里·马蒂亚谢维奇、希拉里·普特南与朱莉娅·罗宾逊证明每个递归可枚举集都是丢番图集，这意味着素数集可由一个多项式公式表示。<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup> 马蒂亚谢维奇于1971年利用斐波那契数勾勒出一个含24个变量、次数为37的素数表示多项式。<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup><sup> • </sup><sup>[16](https://www.mathnet.ru/php/archive.phtml?jrnid=dan&option_lang=eng&paperid=35950&wshow=paper)</sup>

1976年，詹姆斯·P. 琼斯、佐藤大八郎、和田秀男与道格拉斯·温斯明确给出了该多项式：一个在26个变量a、b、c、……、z中次数为25的多项式，当这些变量取遍非负整数时，其正值集合恰好与素数2、3、5、……重合。<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup> 该多项式也取负值，例如 \( -76 \)。<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup> 他们的两种构造都依赖于威尔逊定理，同一篇论文还证明了存在含12个变量的素数表示多项式，以42个变量为代价可做到5次，而最小次数与最少变量数仍是未决问题。<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup>

该多项式是一种编码装置，而非生成器。只有当某个因子等于1时才会出现正值，而这又要求一大组其他多项式条件同时成立；计算它并不能告诉你任何你未曾编码进去的素数信息。<sup>[6](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)</sup>

## 为什么这些公式无助于计算素数

MathWorld援引达德利（1969）与里本博伊姆（1996）指出，这些公式中的每一个都依赖于以极高精度知道某个未知常数，或者要求在使用公式之前已经知道素数。<sup>[1](https://mathworld.wolfram.com/PrimeFormulas.html)</sup> 例如，威兰斯公式把素数隐藏在其指示函数等于1的自变量处，因此计算它相当于对 \( 2^{n} \) 以内的每个整数运行一次素性检验。<sup>[13](https://mathworld.wolfram.com/WillansFormula.html)</sup>

素数的实际计算使用不同的工具。埃拉托斯特尼筛法生成给定上界以下所有素数的列表，阿特金与伯恩斯坦的二元二次筛法是另一种选择。<sup>[8](https://dlmf.nist.gov/27.18)</sup> 对于单个数的素性检验，AKS（阿格拉沃尔–卡亚尔–萨克塞纳）算法是首个确定性多项式时间检验，运行时间为 \( O((\ln n)^{c}) \)，其中c为某常数；ECPP（椭圆曲线素性证明）算法则可处理超过20000位的素数。<sup>[8](https://dlmf.nist.gov/27.18)</sup> 近期关于计算第n个素数的工作表明，对素数计数函数进行二分查找可在 \( O(\sqrt{n} \, (\log n)^{4}) \) 时间内算出 \( p_{n} \)，若假设黎曼假设与克拉默猜想，则可改进为 \( O(\sqrt{n} \cdot (\log^{7/2} n) \cdot \log\log n) \)。<sup>[17](https://arxiv.org/abs/2510.16285)</sup>

公式与算法之间的鸿沟正是本主题的核心教训。素数的精确公式以多种形式存在，但每一个都把素数的困难隐藏在常数、阶乘或方程组之中，而在实际寻找素数方面，没有一个能与筛法和素性检验相竞争。<sup>[1](https://mathworld.wolfram.com/PrimeFormulas.html)</sup><sup> • </sup><sup>[10](https://mathworld.wolfram.com/MillsTheorem.html)</sup>

## References

1. [Prime Formulas, Wolfram MathWorld](https://mathworld.wolfram.com/PrimeFormulas.html)
2. [Christian Elsholtz (2020). Unconditional Prime-representing Functions, Following Mills. arXiv:2004.01285.](https://arxiv.org/abs/2004.01285)
3. [Kota Saito. Mills' constant is irrational. arXiv:2404.19461.](https://arxiv.org/abs/2404.19461)
4. [Saito, K. (2025). Mills' constant is irrational. Mathematika 71(3), e70027.](https://londmathsoc.onlinelibrary.wiley.com/doi/full/10.1112/mtk.70027)
5. [C. P. Willans (1964). On Formulae for the Nth Prime Number. The Mathematical Gazette 48(366), 413–415.](https://www.cambridge.org/core/journals/mathematical-gazette/article/abs/on-formulae-for-the-nth-prime-number/43E49D11DFEAD3E4CBC12F17C87F5EE1)
6. [Jones, J. P., Sato, D., Wada, H., Wiens, D. (1976). Diophantine Representation of the Set of Prime Numbers. American Mathematical Monthly 83(6), 449–464.](https://www.math.umd.edu/~laskow/Pubs/713/Diorepofprimes.pdf)
7. [Formulas for the primes, The Prime Glossary (Chris Caldwell)](https://t5k.org/glossary/page.php?sort=FormulasForPrimes)
8. [DLMF §27.18 Methods of Computation: Primes, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/27.18)
9. [Prime numbers, MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk/HistTopics/Prime_numbers/)
10. [Mills' Theorem, Wolfram MathWorld](https://mathworld.wolfram.com/MillsTheorem.html)
11. [Wright's Fourth Prime, arXiv:1705.09741](https://arxiv.org/abs/1705.09741)
12. [E. M. Wright (1952). The Elementary Proof of the Prime Number Theorem. Proceedings of the Royal Society of Edinburgh Section A 63(3), 257–267.](https://www.cambridge.org/core/journals/proceedings-of-the-royal-society-of-edinburgh-section-a-mathematics/article/abs/xviiithe-elementary-proof-of-the-prime-number-theorem/E63AA8DF602BA83EF1BB65E62369FFBB)
13. [Willans' Formula, Wolfram MathWorld](https://mathworld.wolfram.com/WillansFormula.html)
14. [Willans' Prime Formula implementation, GitHub](https://github.com/valorisa/willans-prime-formula)
15. [Shreevatsa R. Mathematical trolling: the Willans prime formula](https://shreevatsa.net/post/willans-prime-formula/)
16. [Yu. V. Matiyasevich (1971). Diophantine representation of the set of prime numbers. Dokl. Akad. Nauk SSSR 196(4), 770–773.](https://www.mathnet.ru/php/archive.phtml?jrnid=dan&option_lang=eng&paperid=35950&wshow=paper)
17. [Ansh Aggarwal (2025). A Note on Algorithms for Computing \( p_n \). arXiv:2510.16285.](https://arxiv.org/abs/2510.16285)

---
*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: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Computational and probabilistic number theory › Computation of primes and arithmetic functions

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/formula-for-primes

English original: https://www.edgepedia.org/formula-for-primes
