⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sieve.html

📁 数据结构词典(英文)
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD W3 HTML 2.0//EN">
<HTML lang="en-US">
<HEAD>
<TITLE>Sieve of Eratosthenes</TITLE>
<META name="description"
  content="Definition of Sieve of Eratosthenes,
	possibly with links to more information and implementations.">
<META name="keywords" content="Sieve of Eratosthenes">
</HEAD>
<BODY BGCOLOR="#FFFFFF">

<H1>Sieve of Eratosthenes</H1>
<P>
(algorithm)

<P>
<strong>Definition:</strong>
An algorithm to find all prime numbers up to a certain N. Begin with an (unmarked) <a href="array.html" tppabs="http://hissa.nist.gov/dads/HTML/array.html"><em>array</em></a> of integers from 2 to N.  The first unmarked integer, 2, is the first prime.  Mark every multiple of this prime.  Repeatedly take the next unmarked integer as the next prime and mark every multiple of the prime.
<P><em>Note:
Invented by <A HREF="javascript:if(confirm('http://www-groups.dcs.st-and.ac.uk/history/Mathematicians/Eratosthenes.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www-groups.dcs.st-and.ac.uk/history/Mathematicians/Eratosthenes.html'" tppabs="http://www-groups.dcs.st-and.ac.uk/history/Mathematicians/Eratosthenes.html">Eratosthenes of Cyrene</A> (276 BC - 194 BC). <P> For example, here's a beginning array. <pre> &nbsp;2 &nbsp;3 &nbsp;4 &nbsp;5 &nbsp;6 &nbsp;7 &nbsp;8 &nbsp;9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 </pre> Since 2 is unmarked, it is our first prime.  We mark every second integer, that is, 4, 6, 8, 10, 12, etc. <pre> [2] 3 &nbsp;<strike>4</strike> &nbsp;5 &nbsp;<strike>6</strike> &nbsp;7 &nbsp;<strike>8</strike> &nbsp;9 <strike>10</strike> 11 <strike>12</strike> 13 <strike>14</strike> 15 <strike>16</strike> 17 <strike>18</strike> 19 <strike>20</strike> 21 <strike>22</strike> 23 <strike>24</strike> 25 <strike>26</strike> 27 </pre> The next unmarked integer is 3, so it is prime, and we mark every third integer, i.e., 6, 9, 12, etc.  Note that we mark 6, 12, 18, etc. again. <pre> [2] [3] <strike>4</strike> 5 &nbsp;<strike>6</strike> &nbsp;7 &nbsp;<strike>8</strike> &nbsp;<strike>9</strike> <strike>10</strike> 11 <strike>12</strike> 13 <strike>14</strike> <strike>15</strike> <strike>16</strike> 17 <strike>18</strike> 19 <strike>20</strike> <strike>21</strike> <strike>22</strike> 23 <strike>24</strike> 25 <strike>26</strike> <strike>27</strike> </pre> Now 5 is the next prime, and we mark every fifth integer.  The only new integer marked in range is 25. <pre> [2] [3] <strike>4</strike> [5] <strike>6</strike> 7 <strike>8</strike> &nbsp;<strike>9</strike> <strike>10</strike> 11 <strike>12</strike> 13 <strike>14</strike> <strike>15</strike> <strike>16</strike> 17 <strike>18</strike> 19 <strike>20</strike> <strike>21</strike> <strike>22</strike> 23 <strike>24</strike> <strike>25</strike> <strike>26</strike> <strike>27</strike> </pre> From here we find the primes 7, 11, 13, 17, etc. <P> To optimize, when we find the prime n, we can begin marking at n<sup>2</sup>, since anything less than that is n times a lesser prime, so will have been marked earlier.  As a corollary, we can stop marking when n<sup>2</sup> is greater than our range. That is, any unmarked numbers greater than the square root of the range are primes. <P> The naive implementation is not practical for large N since the memory is <a href="theta.html" tppabs="http://hissa.nist.gov/dads/HTML/theta.html"><em> <img src="Theta-1.gif" tppabs="http://hissa.nist.gov/dads/HTML/Images/Theta.gif"  border=0 height=10 width=8  alt="Capital Theta">(N)</em></a>.  More efficient implementations use a <A HREF="javascript:if(confirm('http://www.utm.edu/research/primes/glossary/SieveOfEratosthenes.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.utm.edu/research/primes/glossary/SieveOfEratosthenes.html'" tppabs="http://www.utm.edu/research/primes/glossary/SieveOfEratosthenes.html">segmented sieve</A>.</em>
<P>Author: <a href="terms.html#authorPEB" tppabs="http://hissa.nist.gov/dads/terms.html#authorPEB">PEB</a>
<H2>Implementation</H2>
<A HREF="javascript:if(confirm('http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/programs/Eratosthenes/  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/programs/Eratosthenes/'" tppabs="http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/programs/Eratosthenes/">links to implementations (C, Perl, and Java)</A>, <A HREF="javascript:if(confirm('http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/programs/Eratosthenes/Eratosthenes.pl.txt  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/programs/Eratosthenes/Eratosthenes.pl.txt'" tppabs="http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/programs/Eratosthenes/Eratosthenes.pl.txt">(Perl)</A>, <A HREF="javascript:if(confirm('http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://members.xoom.com/yurtle/proggie/sieve.java  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://members.xoom.com/yurtle/proggie/sieve.java'" tppabs="http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://members.xoom.com/yurtle/proggie/sieve.java">(java)</A>, <A HREF="javascript:if(confirm('http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://kumo.swcp.com/imagine1/ex_sieve.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://kumo.swcp.com/imagine1/ex_sieve.html'" tppabs="http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://kumo.swcp.com/imagine1/ex_sieve.html">(F)</A>, <A HREF="javascript:if(confirm('http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://cat.ucsd.edu:8008/cafeobj/lib/sieve.mod  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://cat.ucsd.edu:8008/cafeobj/lib/sieve.mod'" tppabs="http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://cat.ucsd.edu:8008/cafeobj/lib/sieve.mod">(CafeOBJ)</A>
<H2>More information</H2>
<A HREF="javascript:if(confirm('http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/glossary/SieveOfEratosthenes.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/glossary/SieveOfEratosthenes.html'" tppabs="http://www.nist.gov/cgi-bin/exit_nist.cgi?url=http://www.utm.edu/research/primes/glossary/SieveOfEratosthenes.html">explanation and (pseudocode)</A>

<hr>

Go to the
<A HREF="terms.html" tppabs="http://hissa.nist.gov/dads/terms.html">Algorithms, Data Structures, and Problems</A>
home page.

<hr>

If you have suggestions, corrections, or comments, please get in touch
with
<a href="javascript:if(confirm('http://hissa.nist.gov/~black/black.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://hissa.nist.gov/~black/black.html'" tppabs="http://hissa.nist.gov/~black/black.html">Paul E. Black</a>
&nbsp;(<a href="mailto:paul.black@nist.gov">paul.black@nist.gov</a>).

<p>
Entry modified Tue Dec 21 14:40:06 1999.<BR>
HTML page formatted Wed Dec 22 09:36:29 1999.

<P>
This page's URL is
<A href="sieve.html" tppabs="http://hissa.nist.gov/dads/HTML/sieve.html">http://hissa.nist.gov/dads/HTML/sieve.html</A>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -