294 lines
13 KiB
Text
294 lines
13 KiB
Text
document weave order 0
|
|
head banner <Weave of 'Complete Program' generated by Inweb>
|
|
body
|
|
chapter <Sections>
|
|
chapter header <Sections>
|
|
section <Summing Primes>
|
|
section header <Summing Primes>
|
|
section purpose <Here we verify the conjecture for small numbers.>
|
|
paragraph P1
|
|
material discussion
|
|
commentary <On 7 June 1742, Christian Goldbach wrote a letter from Moscow to Leonhard\n>
|
|
commentary <Euler in Berlin making "eine conjecture hazardiren" that every even number\n>
|
|
commentary <greater than >
|
|
mathematics <2>
|
|
commentary < can be written as a sum of two primes.>
|
|
footnote_cue [1]
|
|
commentary < Euler did not\n>
|
|
commentary <know if this was true, and nor does anyone else.\n>
|
|
figure <Letter.jpg> -1 by 720
|
|
commentary <Goldbach, a professor at St Petersburg and tutor to Tsar Peter II, wrote in\n>
|
|
commentary <several languages in an elegant cursive script, and was much valued as a\n>
|
|
commentary <letter-writer, though his reputation stands less high today.>
|
|
footnote_cue [2]
|
|
commentary < All the same,\n>
|
|
commentary <the general belief now is that primes are just plentiful enough, and just\n>
|
|
commentary <evenly-enough spread, for Goldbach to be right. It is known that:\n>
|
|
item depth 1 label <a>
|
|
commentary <every even number is a sum of at most six primes (Ramaré, 1995), and\n>
|
|
item depth 1 label <b>
|
|
commentary <every odd number is a sum of at most five (Tao, 2012).\n>
|
|
material footnotes
|
|
footnote [1]
|
|
footnote_cue [1]
|
|
commentary < "Greater than 2" is our later proviso: Goldbach needed no such exception\n>
|
|
commentary <because he considered 1 a prime number, as was normal then, and was sometimes\n>
|
|
commentary <said as late as the early twentieth century.\n>
|
|
footnote [2]
|
|
footnote_cue [2]
|
|
commentary < Goldbach, almost exactly a contemporary of Voltaire, was a good citizen\n>
|
|
commentary <of the great age of Enlightenment letter-writing. He and Euler exchanged\n>
|
|
commentary <scholarly letters for over thirty years, not something Euler would have\n>
|
|
commentary <kept up with a duffer. Goldbach was also not, as is sometimes said, a lawyer.\n>
|
|
commentary <See: >
|
|
url content <http://mathshistory.st-andrews.ac.uk/Biographies/Goldbach.html> url <http://mathshistory.st-andrews.ac.uk/Biographies/Goldbach.html>
|
|
commentary <.\n>
|
|
commentary <An edited transcription of the letter is at: >
|
|
url content <http://eulerarchive.maa.org//correspondence/letters/OO0765.pdf> url <http://eulerarchive.maa.org//correspondence/letters/OO0765.pdf>
|
|
commentary <\n>
|
|
paragraph P2
|
|
material discussion
|
|
commentary <Computer verification has been made up to around >
|
|
mathematics <10^{18}>
|
|
commentary <, but by rather\n>
|
|
commentary <better methods than the one we use here. We will only go up to:\n>
|
|
material definition
|
|
code line
|
|
defn <define>
|
|
source_code <RANGE 100>
|
|
_nnnnnpnnn_
|
|
material code: C
|
|
code line
|
|
source_code <#include <stdio.h>>
|
|
_piiiiiiippiiiiipip_
|
|
vskip
|
|
code line
|
|
source_code <int main(int argc, char *argv[]) {>
|
|
_rrrpffffprrrpiiiipprrrrppiiiippppp_
|
|
code line
|
|
source_code < for (int i=4; i<RANGE; i=i+2) >
|
|
_pppprrrpprrrpippppipnnnnnppipipppp_
|
|
commentary < stepping in twos to stay even> (code)
|
|
code line
|
|
source_code < >
|
|
_pppppppp_
|
|
pmac <Solve Goldbach's conjecture for i>
|
|
source_code <;>
|
|
_c_
|
|
code line
|
|
source_code <}>
|
|
_c_
|
|
paragraph P2.1
|
|
material discussion
|
|
commentary <This ought to print:\n>
|
|
material code: ConsoleText
|
|
code line
|
|
source_code < $ goldbach/Tangled/goldbach>
|
|
_ppppepfffffffffffffffffffffffff_
|
|
code line
|
|
source_code < 4 = 2+2>
|
|
_ppppppppppp_
|
|
code line
|
|
source_code < 6 = 3+3>
|
|
_ppppppppppp_
|
|
code line
|
|
source_code < 8 = 3+5>
|
|
_ppppppppppp_
|
|
code line
|
|
source_code < 10 = 3+7 = 5+5>
|
|
_pppppppppppppppppp_
|
|
code line
|
|
source_code < 12 = 5+7>
|
|
_pppppppppppp_
|
|
code line
|
|
source_code < 14 = 3+11 = 7+7>
|
|
_ppppppppppppppppppp_
|
|
code line
|
|
source_code < ...>
|
|
_ppppppp_
|
|
material discussion
|
|
commentary <We'll print each different pair of primes adding up to >
|
|
mathematics <i>
|
|
commentary <. We\n>
|
|
commentary <only check in the range >
|
|
mathematics <2 \leq j \leq i/2>
|
|
commentary < to avoid counting pairs\n>
|
|
commentary <twice over (thus >
|
|
mathematics <8 = 3+5 = 5+3>
|
|
commentary <, but that's hardly two different ways).\n>
|
|
material paragraph macro
|
|
code line
|
|
pmac <Solve Goldbach's conjecture for i> (definition)
|
|
material code: C
|
|
code line
|
|
source_code < printf("%d", i);>
|
|
_ppppiiiiiipssssppipp_
|
|
code line
|
|
source_code < for (int j=2; j<=i/2; j++)>
|
|
_pppprrrpprrrpippppippippppippp_
|
|
code line
|
|
source_code < if ((>
|
|
_pppppppprrppp_
|
|
function usage <isprime>
|
|
source_code <(j)) && (>
|
|
_pippppppp_
|
|
function usage <isprime>
|
|
source_code <(i-j)))>
|
|
_pipippp_
|
|
code line
|
|
source_code < printf(" = %d+%d", j, i-j);>
|
|
_ppppppppppppiiiiiipssssssssssppippipipp_
|
|
code line
|
|
source_code < printf("\n");>
|
|
_ppppiiiiiipsssspp_
|
|
material endnotes
|
|
endnote
|
|
commentary <This code is >
|
|
commentary <used in >
|
|
locale P2
|
|
commentary <.>
|
|
section footer <Summing Primes>
|
|
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 P2.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>
|