# 埃拉托斯特尼筛法

埃拉托斯特尼筛法（Sieve of Eratosthenes）是一种古老的算法，用于找出给定上限以内的所有素数：它列出从2到N的整数，然后反复取下一个未被标记的数作为素数，并划去它的所有倍数，于是只有素数保持未被标记。<sup>[1](https://xlinux.nist.gov/dads/HTML/sieve.html)</sup>该算法归于昔兰尼的埃拉托斯特尼（Eratosthenes of Cyrene，约公元前276—前194年），他是希腊博学家、亚历山大图书馆馆长，而它至今仍是最古老的、仍在实际使用的算法之一。<sup>[2](https://mathshistory.st-andrews.ac.uk/DSB/Eratosthenes.pdf)</sup><sup> • </sup><sup>[3](https://escholarship.org/content/qt4wr015m0/qt4wr015m0_noSplash_9d8cf918cbc9c8f3aa7d65015505d81c.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 归属 | 由昔兰尼的埃拉托斯特尼（约公元前276—前194年）发明；尼科马库斯《算术引论》第一卷第十三章称此法为“筛”（κόσκινον）<sup>[2](https://mathshistory.st-andrews.ac.uk/DSB/Eratosthenes.pdf)</sup><sup> • </sup><sup>[4](https://stoicheiamath.com/wp-content/uploads/2025/09/Nicomachus-Introduction-to-Arithmetic-Student-Edition.pdf)</sup> |
| 最早记载 | 最早由杰拉什的尼科马库斯（Nicomachus of Gerasa）于公元二世纪记录，但归于公元前三世纪；经由波爱修斯（Boethius）传至中世纪<sup>[3](https://escholarship.org/content/qt4wr015m0/qt4wr015m0_noSplash_9d8cf918cbc9c8f3aa7d65015505d81c.pdf)</sup><sup> • </sup><sup>[5](https://www.originalsources.com/Document.aspx?DocID=DHU7DHSUU3F6JP8)</sup> |
| 核心规则 | 当发现素数n时，标记可以从n²开始，因为任何更小的合数都是某个更小素数的倍数，已被标记；一旦n²超出范围，筛除即停止<sup>[1](https://xlinux.nist.gov/dads/HTML/sieve.html)</sup> |
| 复杂度 | O(n log log n)次运算；分段变体使用O(√n)内存<sup>[6](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)</sup> |
| 实际速度 | 在一项覆盖一百万个数的比较研究中，埃拉托斯特尼筛法约1.85秒完成，而阿特金筛法（Sieve of Atkin）耗时3.39秒<sup>[7](https://scienceinsights.org/what-is-the-sieve-of-eratosthenes-and-how-does-it-work/)</sup> |
| 现代形式 | 带轮式分解的分段筛法，配合位数组与缓存大小的分段，如primesieve库中所实现<sup>[6](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)</sup> |

## 算法

过程直截了当。先准备一个从2到N的整数数组，全部未标记。第一个未标记的整数2是素数；标记2的所有倍数。下一个未标记的整数3是下一个素数；标记每第三个数，即6、9、12等等（6、12、18被第二次标记）。下一个未标记的整数5是素数，而在不超过27的范围内，标记每第五个数只新增了25这一个新条目；其余未标记的数7、11、13、17、19、23都是素数。<sup>[1](https://xlinux.nist.gov/dads/HTML/sieve.html)</sup>

两项优化限定了工作量。第一，当发现素数n时，标记可以从n²开始：任何小于n²的合数都是某个更小素数的倍数，早已被标记。作为推论，一旦n²超出范围，筛除便可停止，因此只需用到√N以内的素数就能筛完整张表；√N以上所有未标记的数都是素数。<sup>[1](https://xlinux.nist.gov/dads/HTML/sieve.html)</sup>第二，尼科马库斯所描述的古典希腊版本只处理奇数：先写出从3开始的奇数，然后从3起数，跳过两个数、划去第三个（3的倍数），再对5重复这一过程，跳过四个数、划去第五个，依此类推；对每个奇数n，跳过n−1个数、划去下一个，最后剩下的数全是素数。<sup>[2](https://mathshistory.st-andrews.ac.uk/DSB/Eratosthenes.pdf)</sup><sup> • </sup><sup>[4](https://stoicheiamath.com/wp-content/uploads/2025/09/Nicomachus-Introduction-to-Arithmetic-Student-Edition.pdf)</sup>

在古代的记载中，这个名字是埃拉托斯特尼自己起的：尼科马库斯写道，这些数的产生被称为“筛”，因为从混杂在一起、不加区分的奇数中，此法像借助某种工具或筛子一样，把素数与不可分解数、次级数与合数、以及混合的一类分离开来。<sup>[4](https://stoicheiamath.com/wp-content/uploads/2025/09/Nicomachus-Introduction-to-Arithmetic-Student-Edition.pdf)</sup>

## 古代与中世纪的流传

筛法是已知最古老的生成连续素数的算法，它用手工运行和用计算机运行一样容易。<sup>[8](https://acta.sapientia.ro/content/docs/cache-optimized-linear-sieve.pdf)</sup>我们对它的了解来自杰拉什的尼科马库斯，这位新毕达哥拉斯派哲学家在其《算术引论》（公元二世纪初）第一卷第十三章中介绍了筛法；该书于二世纪由阿普列尤斯（Apuleius）译成拉丁文，又于五或六世纪由波爱修斯翻译，正是通过波爱修斯，关于筛法的记述传到了中世纪。<sup>[5](https://www.originalsources.com/Document.aspx?DocID=DHU7DHSUU3F6JP8)</sup><sup> • </sup><sup>[9](https://www.uni-erfurt.de/en/university/current/news/news-detail/neue-publikation-nikomachos-von-gerasa-einfuehrung-in-die-arithmetik)</sup>数学史家托马斯·希思（Thomas Heath）在其《希腊数学史》中简洁地概括了尼科马库斯那段颇为冗长的记述。<sup>[5](https://www.originalsources.com/Document.aspx?DocID=DHU7DHSUU3F6JP8)</sup>

埃拉托斯特尼本人还以亚历山大图书馆首席馆长身份闻名，他根据亚历山大港与赛伊尼（Syene）两地日晷的影子测出了地球的周长，并创立了科学年代学；筛法只是他广博的科学成果中的一部分。<sup>[10](https://academic.oup.com/book/46704)</sup><sup> • </sup><sup>[2](https://mathshistory.st-andrews.ac.uk/DSB/Eratosthenes.pdf)</sup>

## 复杂度与内存

埃拉托斯特尼筛法找出n以内所有素数需要O(n log log n)次运算，而朴素版本需要与n成正比的内存，这对很大的N并不实用。<sup>[1](https://xlinux.nist.gov/dads/HTML/sieve.html)</sup><sup> • </sup><sup>[6](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)</sup>分段是标准的补救办法，按primesieve文档的评价，也是已知对筛法最好的实用改进：不是一次筛完[2, n]，而是把区间分成大小相等的段依次筛除，从而将内存需求从O(n)降到O(√n)。分段版本最早由R. C. 辛格尔顿（R. C. Singleton）于1969年发表。<sup>[6](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)</sup>

分段大小之所以重要，原因在于古代算法不可能预见到的一点：在现代CPU上，用大素数筛除时内存访问步幅很长，访问不是顺序的，程序大部分时间都耗在缓存未命中后等待数据读取上。<sup>[8](https://acta.sapientia.ro/content/docs/cache-optimized-linear-sieve.pdf)</sup>因此实际实现会选择适合CPU一级或二级缓存的分段大小，把筛子打包成位数组（例如每30个数用8个标志位，恰好对应一个字节的8位），并应用轮式分解，完全跳过小素数的倍数：第一类轮只考虑奇数，模6轮跳过2和3的倍数，模30轮跳过2、3、5的倍数，依此类推。<sup>[6](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)</sup><sup> • </sup><sup>[11](https://wwwhomes.uni-bielefeld.de/achim/prime_sieve.html)</sup>最近一种结合分段、位打包与缓存行对齐块处理的缓存感知混合筛法，据报道内存使用最多降低为原来的八分之一，运行速度最多达到经典筛法的2.4倍、分段筛法的1.7倍。<sup>[12](https://arxiv.org/abs/2601.19909)</sup>空间还可以进一步压缩：有一种构造在空间O(N<sup>1/3</sup>(log N)<sup>2/3</sup>)、时间O(N log N)内完成对N以内的埃拉托斯特尼筛法，优于通常需要约O(√N)空间的版本。<sup>[13](https://arxiv.org/abs/1712.09130)</sup>

## 与其他筛法的比较

阿特金筛法由阿特金（Atkin）与伯恩斯坦（Bernstein）于2004年发表，它使用二元二次型和模条件来判定素性，而不是划去素数的倍数；其证明比埃拉托斯特尼筛法的证明长得多，也不那么显然。<sup>[14](https://www.geeksforgeeks.org/dsa/sieve-of-atkin/)</sup><sup> • </sup><sup>[15](https://cr.yp.to/papers/primesieves-20020329-retypeset20220327.pdf)</sup>在实践中，经典方法往往胜出：在一项覆盖一百万个数的比较研究中，埃拉托斯特尼筛法约1.85秒完成，阿特金筛法为3.39秒；而且由于运行时间中的log log n因子增长极慢，筛法对所有实际规模而言实际上都很快。<sup>[7](https://scienceinsights.org/what-is-the-sieve-of-eratosthenes-and-how-does-it-work/)</sup>另一种替代方案孙达拉姆筛法（Sieve of Sundaram）的时间复杂度为O(n log n)、空间复杂度为O(n)，因此就生成给定上限以内的素数而言，效率不及埃拉托斯特尼筛法。<sup>[16](https://pmc.ncbi.nlm.nih.gov/articles/PMC11567616/)</sup>

关于命名的警示来自函数式编程。那段简短、被广泛教授的Haskell单行代码，从惰性表中过滤掉每个存活数的倍数，常被当作埃拉托斯特尼筛法来介绍，但哈维穆德学院（Harvey Mudd College）的计算机科学家梅利莎·E. 奥尼尔（Melissa E. O'Neill）在2009年《函数式编程杂志》的一篇论文中表明，这个算法实际上是试除法，而非筛法，并给出了一种真正类似筛法的惰性实现，使用了筛法所需的那类数据结构。<sup>[17](https://doi.org/10.1017/S0956796808007004)</sup>

## 现状

筛法仍是实际最快的素数生成器的基础。primesieve程序使用带轮式分解的分段埃拉托斯特尼筛法生成素数，预先筛去163以内小素数的倍数，使用模210轮，并支持多线程；其速度主要来自分段（可避免缓存未命中）以及使用位数组而非布尔数组。<sup>[6](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)</sup>在渐近意义上，筛法的O(n log log n)界作为基准屹立了两千多年：一篇2026年的论文描述了新算法，首次以log N的正幂次超越了古代筛法，比此前最佳的普里查德（Pritchard，1981）、阿特金与伯恩斯坦（2004）以及谢尔盖耶夫（Sergeev，2016）的算法提升了近log N倍。<sup>[18](https://arxiv.org/abs/2606.22851)</sup>素数生成这一任务在数论以及密码学中仍居核心地位，在密码学中，筛法被用来在素性检验之前排除可被小素数整除的候选数。<sup>[12](https://arxiv.org/abs/2601.19909)</sup><sup> • </sup><sup>[19](https://standards.iteh.ai/catalog/standards/iso/561d5998-8df0-4611-a4c9-dfc0b26ca881/iso-iec-18032-2020)</sup>

## References

1. [Paul E. Black, "sieve of Eratosthenes", Dictionary of Algorithms and Data Structures, NIST](https://xlinux.nist.gov/dads/HTML/sieve.html)
2. [Dictionary of Scientific Biography: Eratosthenes of Cyrene, MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk/DSB/Eratosthenes.pdf)
3. [Redmond McNamara, A Dynamical Proof of the Prime Number Theorem (dissertation, University of California)](https://escholarship.org/content/qt4wr015m0/qt4wr015m0_noSplash_9d8cf918cbc9c8f3aa7d65015505d81c.pdf)
4. [Nicomachus of Gerasa, Introduction to Arithmetic, Book I, Ch. 13 (student edition translation)](https://stoicheiamath.com/wp-content/uploads/2025/09/Nicomachus-Introduction-to-Arithmetic-Student-Edition.pdf)
5. [A Source Book in Medieval Science, Original Sources (sieve of Eratosthenes via Nicomachus and Boethius)](https://www.originalsources.com/Document.aspx?DocID=DHU7DHSUU3F6JP8)
6. [kimwalisch, primesieve: ALGORITHMS.md](https://github.com/kimwalisch/primesieve/blob/master/doc/ALGORITHMS.md)
7. [What Is the Sieve of Eratosthenes and How Does It Work?, ScienceInsights](https://scienceinsights.org/what-is-the-sieve-of-eratosthenes-and-how-does-it-work/)
8. [A. Járai, E. Vatai, Cache optimized linear sieve, Acta Universitatis Sapientiae, Informatica](https://acta.sapientia.ro/content/docs/cache-optimized-linear-sieve.pdf)
9. [University of Erfurt: Nikomachos von Gerasa, Einführung in die Arithmetik (Brodersen translation)](https://www.uni-erfurt.de/en/university/current/news/news-detail/neue-publikation-nikomachos-von-gerasa-einfuehrung-in-die-arithmetik)
10. [Eratosthenes and the Measurement of the Earth's Circumference (c. 230 BC), Oxford Academic](https://academic.oup.com/book/46704)
11. [Achim Flammenkamp, Sieve of Eratosthenes](https://wwwhomes.uni-bielefeld.de/achim/prime_sieve.html)
12. [A Cache-Aware Hybrid Sieve Combining Segmentation and Bit-Packing for Fast Prime Generation, arXiv](https://arxiv.org/abs/2601.19909)
13. [An improved sieve of Eratosthenes, arXiv](https://arxiv.org/abs/1712.09130)
14. [Sieve of Atkin, GeeksforGeeks](https://www.geeksforgeeks.org/dsa/sieve-of-atkin/)
15. [A. O. L. Atkin, D. J. Bernstein, Prime sieves using binary quadratic forms](https://cr.yp.to/papers/primesieves-20020329-retypeset20220327.pdf)
16. [Computational challenges and solutions: Prime number generation for enhanced data security, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC11567616/)
17. [Melissa E. O'Neill, The Genuine Sieve of Eratosthenes, Journal of Functional Programming 19 (1), 2009](https://doi.org/10.1017/S0956796808007004)
18. [Faster enumeration of primes, arXiv](https://arxiv.org/abs/2606.22851)
19. [ISO/IEC 18032:2020, Prime Number Generation for Cryptographic Applications](https://standards.iteh.ai/catalog/standards/iso/561d5998-8df0-4611-a4c9-dfc0b26ca881/iso-iec-18032-2020)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Elementary number theory › Prime numbers: elementary aspects

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/sieve-of-eratosthenes

English original: https://www.edgepedia.org/sieve-of-eratosthenes
