149 lines
6.3 KiB
Text
149 lines
6.3 KiB
Text
document weave order 0
|
|
head banner <Weave of 'The Sieve of Eratosthenes' generated by Inweb>
|
|
body
|
|
chapter <Sections>
|
|
chapter header <Sections>
|
|
section <The Sieve of Eratosthenes>
|
|
section header <The Sieve of Eratosthenes>
|
|
section purpose <A fairly fast way to determine if small numbers are prime, given storage.>
|
|
toc - <S/tsoe>
|
|
toc line - <S1, Storage> P1'Storage'
|
|
toc line - <S2, Primality> P2'Primality'
|
|
paragraph P1'Storage'
|
|
material discussion
|
|
commentary <This technique, still essentially the best sieve for finding prime\n>
|
|
commentary <numbers, is attributed to Eratosthenes of Cyrene and dates from the 200s BC.\n>
|
|
commentary <Since composite numbers are exactly those numbers which are multiples of\n>
|
|
commentary <something, the idea is to remove everything which is a multiple: whatever\n>
|
|
commentary <is left, must be prime.\n>
|
|
vskip (in comment)
|
|
commentary <This is very fast (and can be done more quickly than the implementation\n>
|
|
commentary <below), but (a) uses storage to hold the sieve, and (b) has to start right\n>
|
|
commentary <back at 2 - so it can't efficiently test just, say, the eight-digit numbers\n>
|
|
commentary <for primality.\n>
|
|
material code: C
|
|
code line
|
|
source_code <int still_in_sieve[RANGE + 1];>
|
|
_rrrpiiiiiiiiiiiiiipnnnnnpppnpp_
|
|
code line
|
|
source_code <int sieve_performed = FALSE;>
|
|
_rrrpiiiiiiiiiiiiiiipppnnnnnp_
|
|
paragraph P2'Primality'
|
|
material discussion
|
|
commentary <We provide this as a function which determines whether a number is prime:\n>
|
|
material definition
|
|
code line
|
|
defn <define>
|
|
source_code <TRUE 1>
|
|
_nnnnpn_
|
|
code line
|
|
defn <define>
|
|
source_code <FALSE 0>
|
|
_nnnnnpn_
|
|
material code: C
|
|
code line
|
|
source_code <int >
|
|
_rrrp_
|
|
function defn <isprime>
|
|
commentary <Summing Primes>
|
|
commentary < - >
|
|
locale P3.1
|
|
source_code <(int n) {>
|
|
_prrrpippp_
|
|
code line
|
|
source_code < if (n <= 1) return FALSE;>
|
|
_pppprrppippppnpprrrrrrpnnnnnp_
|
|
code line
|
|
source_code < if (n > RANGE) { printf("Out of range!\n"); return FALSE; }>
|
|
_pppprrppipppnnnnnppppiiiiiipsssssssssssssssssppprrrrrrpnnnnnppp_
|
|
code line
|
|
source_code < if (!sieve_performed) >
|
|
_pppprrpppiiiiiiiiiiiiiiipp_
|
|
pmac <Perform the sieve>
|
|
source_code <;>
|
|
_p_
|
|
code line
|
|
source_code < return still_in_sieve[n];>
|
|
_pppprrrrrrpiiiiiiiiiiiiiipipp_
|
|
code line
|
|
source_code <}>
|
|
_p_
|
|
paragraph P2.1
|
|
material discussion
|
|
commentary <We save a little time by noting that if a number up to >
|
|
inline
|
|
source_code <RANGE>
|
|
_xxxxx_
|
|
commentary < is composite\n>
|
|
commentary <then one of its factors must be smaller than the square root of >
|
|
inline
|
|
source_code <RANGE>
|
|
_xxxxx_
|
|
commentary <. Thus,\n>
|
|
commentary <in a sieve of size 10000, one only needs to remove multiples of 2 up to 100,\n>
|
|
commentary <for example.\n>
|
|
material paragraph macro
|
|
code line
|
|
pmac <Perform the sieve> (definition)
|
|
material code: C
|
|
code line
|
|
source_code < >
|
|
_pppp_
|
|
pmac <Start with all numbers from 2 upwards in the sieve>
|
|
source_code <;>
|
|
_p_
|
|
code line
|
|
source_code < for (int n=2; n*n <= RANGE; n++)>
|
|
_pppprrrpprrrpippppipippppnnnnnppippp_
|
|
code line
|
|
source_code < if (still_in_sieve[n])>
|
|
_pppppppprrppiiiiiiiiiiiiiipipp_
|
|
code line
|
|
source_code < >
|
|
_pppppppppppp_
|
|
pmac <Shake out multiples of n>
|
|
source_code <;>
|
|
_p_
|
|
code line
|
|
source_code < sieve_performed = TRUE;>
|
|
_ppppiiiiiiiiiiiiiiipppnnnnp_
|
|
material endnotes
|
|
endnote
|
|
commentary <This code is >
|
|
commentary <used in >
|
|
locale P2'Primality'
|
|
commentary <.>
|
|
paragraph P2.1.1
|
|
material paragraph macro
|
|
code line
|
|
pmac <Start with all numbers from 2 upwards in the sieve> (definition)
|
|
material code: C
|
|
code line
|
|
source_code < still_in_sieve[1] = FALSE;>
|
|
_ppppiiiiiiiiiiiiiippppppnnnnnp_
|
|
code line
|
|
source_code < for (int n=2; n <= RANGE; n++) still_in_sieve[n] = TRUE;>
|
|
_pppprrrpprrrpippppippppnnnnnppippppiiiiiiiiiiiiiipippppnnnnp_
|
|
material endnotes
|
|
endnote
|
|
commentary <This code is >
|
|
commentary <used in >
|
|
locale P2.1
|
|
commentary <.>
|
|
paragraph P2.1.2
|
|
material paragraph macro
|
|
code line
|
|
pmac <Shake out multiples of n> (definition)
|
|
material code: C
|
|
code line
|
|
source_code < for (int m= n+n; m <= RANGE; m += n) still_in_sieve[m] = FALSE;>
|
|
_pppprrrpprrrpippipippippppnnnnnppippppippiiiiiiiiiiiiiipippppnnnnnp_
|
|
material endnotes
|
|
endnote
|
|
commentary <This code is >
|
|
commentary <used in >
|
|
locale P2.1
|
|
commentary <.>
|
|
section footer <The Sieve of Eratosthenes>
|
|
chapter footer <Sections>
|
|
tail rennab <End of weave>
|