2020-04-12 16:24:23 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html >
< head >
< title > Complete Program< / title >
< meta name = "viewport" content = "width=device-width initial-scale=1" >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" >
< meta http-equiv = "Content-Language" content = "en-gb" >
< link href = "../inweb.css" rel = "stylesheet" rev = "stylesheet" type = "text/css" > < style type = "text/css" >
/* This is a CSS file for pages woven by the literate programming tool inweb */
/* We'll start with syntax colouring, since everyone likes to toy with this: */
.cwebmacrotext { color: #000000; font-weight: bold; } /* The name of a web macro */
.functiontext { color: #C00000; } /* When a C function is being defined */
.string { color: #408040; } /* A double-quoted C literal string */
.character { color: #204020; } /* A single-quoted C literal character */
.constant { color: #204020; } /* A named constant */
.element { color: #40407f; } /* A C structure element */
.identifier { color: #4040ff; } /* An identifier */
.reserved { color: #600000; } /* A reserved word */
/* Comments at the end of lines are rendered like so: */
.comment {
font: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif;
color: #404040;
font-style:italic;
}
/* The name of a web macro when being used: */
.cwebmacro {
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif;
text-decoration: none;
color: #000000; font-weight: bold;
}
.cwebmacronumber {
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif;
font-size: 80%;
text-decoration: none;
color: #000000;
}
/* And when being defined: */
.cwebmacrodefn {
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif;
text-decoration: none;
color: #000000; font-weight: bold;
font-weight:bold;
}
/* Now some generalities: */
body {
background-color: #ffffff;
margin: 8px;
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif;
font-size: 16px;
line-height: 24px;
background-color: transparent;
-webkit-font-smoothing: antialiased;
}
/* Forms of paragraph: */
p {
margin: 0;
padding-top: 5px;
padding-bottom: 5px;
clear: both;
}
/* Where an inweb paragraph begins: */
p.inwebparagraph {
width: 60em;
line-height: 150%;
padding-top: 15px;
padding-bottom: 5px;
}
/* If a web macro definition begins in its own paragraph, this is it: */
p.macrodefinition {
padding-top: 0px;
padding-bottom: 0px;
margin-left: 30px;
}
/* The endnotes at the foot of code: "The function foo_bar is used in ..." */
p.endnote {
font-size: 0.8em;
padding-top: 0px;
padding-bottom: 0px;
margin-left: 30px;
text-decoration:none;
color: #000000;
}
/* The Purpose slug: */
p.purpose {
width: 60em;
line-height: 150%;
font-size: 1em;
font-style:italic;
margin-left: 15px;
}
/* Blocks of code appear in these */
pre {
font-family: Monaco, monospace;
}
pre.display {
padding: 3px;
margin-top: 5px;
margin-bottom: 5px;
margin-left: 30px;
margin-right: 10px;
background: #f8f8f8;
border-width: 1px;
border-style: solid solid solid solid;
border-color: #ccc;
word-wrap: break-word;
white-space: pre-wrap;
}
pre.displaydefn {
padding: 3px;
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
margin-right: 10px;
background: #f8f8f8;
border-width: 1px;
border-style: solid solid solid solid;
border-color: #ccc;
word-wrap: break-word;
white-space: pre-wrap;
}
pre.definitions {
padding-top: 0px;
margin-top: 0px;
margin-left: 0px;
margin-right: 10px;
word-wrap: break-word;
white-space: pre-wrap;
}
/* The following are for breadcrumbs of links: */
ul, li {
list-style-type:none;
padding:0;
margin:0;
}
.crumbs {
border:1px solid #dedede;
height:3.45em;
}
.crumbs li {
float:left;
line-height:2.3em;
padding-left:.75em;
color:#333;
font-size: 1.5em;
}
.crumbs li a {
display:block;
padding:0 15px 0 0;
background:url(crumbs.gif) no-repeat right center;
}
.crumbs li a:link, #crumbs li a:visited {
text-decoration:none;
color:#333;
}
.crumbs li a:hover, #crumbs li a:focus {
color:#dd2c0d;
}
/* This is for items in lists from inweb source: */
ul.items {
margin-top: 3px;
margin-bottom: 3px;
}
ul.items li {
margin-top: 1px;
margin-bottom: 1px;
margin-left: 15px;
}
/* And this is for each section's table of contents: */
ul.toc {
margin-top: 3px;
margin-bottom: 3px;
}
ul.toc li {
float:left;
margin-left: 15px;
color:#000;
}
ul.toc li a {
display:block;
}
ul.toc li a:link, ul.toc li a:visited {
text-decoration:none;
color:#000;
}
ul.toc li a:hover, ul.toc li a:focus {
color:#dd2c0d;
}
hr.tocbar {
clear: both;
margin-top:3px;
margin-bottom:3px;
color:#333;
border:0
background-color:#333;
}
/* This is for cross-reference links ("This code is used in ...") */
p.endnote:link, p.endnote:visited, p.endnote:hover, p.endnote:focus {
text-decoration:none;
color:#000;
}
p.endnote a:link, p.endnote a:visited {
text-decoration:none;
color:#000;
}
p.endnote a:hover, p.endnote a:focus {
text-decoration:none;
color:#dd2c0d;
}
/* Now for the index page */
p.heading {
margin-top: 2px;
margin-bottom: 2px;
font-size: 20
}
p.chapter {
font-size: 14
}
p.tight {
font-size: 12
}
p.tightin {
padding-left: 25px;
font-size: 12
}
.chapterlist li {
padding-top: 8px;
padding-left: 16px;
}
.chaptertitle {
font-weight: bold;
}
.chapterlist .sectionlist li {
padding-left: 48px;
}
span.definitionkeyword {
color:#801010;
font-weight:bold;
}
span.extract {
font-size: 15px;
border: 1px solid #e8e8e8;
border-radius: 3px;
background-color: #eef;
width: 100%;
padding: 3px;
}
nav[role="navigation"] {
width: 180px;
position: fixed;
overflow: scroll;
left: 0;
top: 0;
bottom: 0;
background: #FAFAFA;
border-right: 1px solid #e6e6e6;
padding: 20px 30px
}
nav[role="navigation"] ul {
border-top: 1px solid #e6e6e6;
font-weight: 400;
margin-bottom: 30px;
list-style: none
}
nav[role="navigation"] ul ul {
list-style: none
}
nav[role="navigation"] ul li {
border-bottom: 1px solid #e6e6e6
}
nav[role="navigation"] ul li.active {
border-bottom: 1px solid #333
}
nav[role="navigation"] ul li.active a {
font-weight: 700
}
nav[role="navigation"] h1 a:link {
color: #333;
text-decoration: none;
display: block;
}
nav[role="navigation"] h1 a:visited {
color: #333
}
nav[role="navigation"] h1 a:active {
font-weight: 700
}
nav[role="navigation"] h1 a:hover {
color: #dd2c0d
}
nav[role="navigation"] ul li a:link {
color: #333;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 1px;
font-size: 12px;
display: block;
padding: 10px 10px 2px 2px
}
2020-04-13 16:06:45 +00:00
nav[role="navigation"] ul li span.selectedlink {
color: #f25;
}
2020-04-12 16:24:23 +00:00
nav[role="navigation"] ul li span.unlink {
color: #f25;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 1px;
font-size: 12px;
display: block;
padding: 10px 10px 2px 2px
}
nav[role="navigation"] ul li a:visited {
color: #333
}
nav[role="navigation"] ul li a:active {
font-weight: 700
}
nav[role="navigation"] ul li a:hover {
color: #dd2c0d
}
nav[role="navigation"] ul li ul {
margin-bottom: 10px;
border-top: none
}
nav[role="navigation"] ul li ul li {
border-bottom: none;
padding: 0.1em
}
nav[role="navigation"] ul li ul li.active {
border-bottom: none
}
nav[role="navigation"] ul li ul li.active a {
font-weight: 700
}
nav[role="navigation"] ul li ul a:link {
color: #dd2c0d;
text-decoration: none;
text-transform: none;
letter-spacing: 0;
font-size: 12px;
display: block;
margin-left: 15px;
padding: 0 0 3px;
border-bottom: none;
font-weight: 300
}
nav[role="navigation"] ul li ul a:hover {
text-decoration: underline
}
nav[role="navigation"] h2 {
font-size: 0.95em;
text-transform: lowercase;
font-variant: small-caps;
color: #999;
padding-bottom: 0.5em
}
nav[role="navigation"] h1 {
margin-top: 0em
}
main {
max-width: 1024px;
min-width: 320px;
margin-left: 250px;
min-height: 100%;
height: auto !important;
height: 100%
}
#logo {
text-indent: -9999px;
height: 48px;
width: 100%;
margin-top: 20px;
margin-bottom: 0.5em;
padding-bottom: 10px
}
#logo a {
display: block;
width: 190px;
height: 48px;
background-image: url("/assets/images/swift.svg");
background-repeat: no-repeat;
background-size: 190px 48px;
background-position-x: -8px
}
< / style >
< / head >
< body >
< nav role = "navigation" >
2020-04-13 16:06:45 +00:00
< h1 > < a href = "../index.html" >
< img src = "../..//docs/docs-src/Figures/Octagram184x184.png" width = 72 height = 72" >
< / a > < / h1 >
< ul > < li > < a href = "../inweb/index.html" > inweb< / a > < / li >
< / ul > < h2 > Foundation Module< / h2 > < ul >
< li > < a href = "../foundation-module/index.html" > foundation< / a > < / li >
2020-04-12 16:24:23 +00:00
< li > < a href = "../foundation-test/index.html" > foundation-test< / a > < / li >
2020-04-13 16:06:45 +00:00
< / ul > < h2 > Example Webs< / h2 > < ul >
2020-04-12 16:24:23 +00:00
< li > < a href = "../goldbach/index.html" > goldbach< / a > < / li >
2020-04-13 16:06:45 +00:00
< li > < span class = "unlink" > twinprimes< / span > < / li >
< / ul > < h2 > External< / h2 > < ul >
< li > < a href = "https://github.com/ganelson/inweb" > github< / a > < / li >
2020-04-12 16:24:23 +00:00
2020-04-13 16:06:45 +00:00
< / ul >
2020-04-12 16:24:23 +00:00
< / nav >
< main role = "main" >
<!-- Weave of 'Complete Program' generated by 7 -->
2020-04-13 22:30:42 +00:00
< ul class = "crumbs" > < li > < a href = "../index.html" > Home< / a > < / li > < li > < b > twinprimes< / b > < / li > < / ul > < p class = "inwebparagraph" > < a id = "SP1" > < / a > < b > § 1. The conjecture. < / b > It is widely believed that there are an infinite number of twin primes, that
2020-04-12 16:24:23 +00:00
is, prime numbers occurring in pairs different by 2. Twins are known to exist
at least as far out as \(10^{388,342}\) (as of 2016), and there are infinitely
many pairs of primes closer together than about 250 (Zhang, 2013; Tao, Maynard,
and many others, 2014).
< / p >
< p class = "inwebparagraph" > This program finds a few small pairs of twins, by the simplest method possible,
and should print output like so:
< / p >
< pre class = "display" >
< span class = "plain" > 3 and 5< / span >
< span class = "plain" > 5 and 7< / span >
< span class = "plain" > 11 and 13< / span >
< span class = "plain" > ...< / span >
< / pre >
< p class = "inwebparagraph" > < / p >
< pre class = "definitions" >
< span class = "definitionkeyword" > define< / span > < span class = "constant" > RANGE< / span > < span class = "plain" > < / span > < span class = "constant" > 100< / span > < span class = "plain" > < / span > < span class = "comment" > the upper limit to the numbers we will consider< / span >
< / pre >
< pre class = "display" >
< span class = "plain" > #< / span > < span class = "identifier" > include< / span > < span class = "plain" > < < / span > < span class = "identifier" > stdio< / span > < span class = "plain" > .< / span > < span class = "identifier" > h< / span > < span class = "plain" > > < / span >
< span class = "reserved" > int< / span > < span class = "plain" > < / span > < span class = "functiontext" > main< / span > < span class = "plain" > (< / span > < span class = "reserved" > int< / span > < span class = "plain" > < / span > < span class = "identifier" > argc< / span > < span class = "plain" > , < / span > < span class = "reserved" > char< / span > < span class = "plain" > *< / span > < span class = "identifier" > argv< / span > < span class = "plain" > []) {< / span >
< span class = "reserved" > for< / span > < span class = "plain" > (< / span > < span class = "reserved" > int< / span > < span class = "plain" > < / span > < span class = "identifier" > i< / span > < span class = "plain" > =1; < / span > < span class = "identifier" > i< / span > < span class = "plain" > < < / span > < span class = "constant" > RANGE< / span > < span class = "plain" > ; < / span > < span class = "identifier" > i< / span > < span class = "plain" > ++)< / span >
< < span class = "cwebmacro" > Test for twin prime at i< / span > < span class = "cwebmacronumber" > 1.1< / span > > < span class = "plain" > ;< / span >
< span class = "plain" > }< / span >
< / pre >
< p class = "inwebparagraph" > < / p >
< p class = "inwebparagraph" > < a id = "SP1_1" > < / a > < b > § 1.1. < / b > < code class = "display" >
< < span class = "cwebmacrodefn" > Test for twin prime at i< / span > < span class = "cwebmacronumber" > 1.1< / span > > =
< / code > < / p >
< pre class = "displaydefn" >
< span class = "reserved" > if< / span > < span class = "plain" > ((< / span > < span class = "functiontext" > < a href = "#SP2" > isprime< / a > < / span > < span class = "plain" > (< / span > < span class = "identifier" > i< / span > < span class = "plain" > )) & & (< / span > < span class = "functiontext" > < a href = "#SP2" > isprime< / a > < / span > < span class = "plain" > (< / span > < span class = "identifier" > i< / span > < span class = "plain" > +2)))< / span >
< span class = "identifier" > printf< / span > < span class = "plain" > (< / span > < span class = "string" > "%d and %d\n"< / span > < span class = "plain" > , < / span > < span class = "identifier" > i< / span > < span class = "plain" > , < / span > < span class = "identifier" > i< / span > < span class = "plain" > +2);< / span >
< / pre >
< p class = "inwebparagraph" > < / p >
< p class = "endnote" > This code is used in < a href = "#SP1" > § 1< / a > .< / p >
< p class = "inwebparagraph" > < a id = "SP2" > < / a > < b > § 2. Primality. < / b > This simple and slow test tries to divide by every whole number at least
2 and up to the square root: if none divide exactly, the number is prime.
A common error with this algorithm is to check where \(m^2 < n\), rather
than \(m^2 \leq n\), thus wrongly considering 4, 9, 25, 49, ... as prime:
Cambridge folklore has it that this bug occurred on the first computation
of the EDSAC computer on 6 May 1949.
< / p >
< pre class = "definitions" >
< span class = "definitionkeyword" > define< / span > < span class = "constant" > TRUE< / span > < span class = "plain" > < / span > < span class = "constant" > 1< / span >
< span class = "definitionkeyword" > define< / span > < span class = "constant" > FALSE< / span > < span class = "plain" > < / span > < span class = "constant" > 0< / span >
< / pre >
< pre class = "display" >
< span class = "reserved" > int< / span > < span class = "plain" > < / span > < span class = "functiontext" > isprime< button class = "popup" onclick = "togglePopup('usagePopup0')" > ...< span class = "popuptext" id = "usagePopup0" > Usage of < b > isprime< / b > :< br > < a href = "#SP1_1" > § 1.1< / a > < / span > < / button > < / span > < span class = "plain" > (< / span > < span class = "reserved" > int< / span > < span class = "plain" > < / span > < span class = "identifier" > n< / span > < span class = "plain" > ) {< / span >
< span class = "reserved" > if< / span > < span class = "plain" > (< / span > < span class = "identifier" > n< / span > < span class = "plain" > < = < / span > < span class = "constant" > 1< / span > < span class = "plain" > ) < / span > < span class = "reserved" > return< / span > < span class = "plain" > < / span > < span class = "constant" > FALSE< / span > < span class = "plain" > ;< / span >
< span class = "reserved" > for< / span > < span class = "plain" > (< / span > < span class = "reserved" > int< / span > < span class = "plain" > < / span > < span class = "identifier" > m< / span > < span class = "plain" > = < / span > < span class = "constant" > 2< / span > < span class = "plain" > ; < / span > < span class = "identifier" > m< / span > < span class = "plain" > *< / span > < span class = "identifier" > m< / span > < span class = "plain" > < = < / span > < span class = "identifier" > n< / span > < span class = "plain" > ; < / span > < span class = "identifier" > m< / span > < span class = "plain" > ++)< / span >
< span class = "reserved" > if< / span > < span class = "plain" > (< / span > < span class = "identifier" > n< / span > < span class = "plain" > % < / span > < span class = "identifier" > m< / span > < span class = "plain" > == < / span > < span class = "constant" > 0< / span > < span class = "plain" > )< / span >
< span class = "reserved" > return< / span > < span class = "plain" > < / span > < span class = "constant" > FALSE< / span > < span class = "plain" > ;< / span >
< span class = "reserved" > return< / span > < span class = "plain" > < / span > < span class = "constant" > TRUE< / span > < span class = "plain" > ;< / span >
< span class = "plain" > }< / span >
< / pre >
< p class = "inwebparagraph" > < / p >
<!-- End of weave -->
< script >
MathJax = {
tex: {
2020-04-13 16:06:45 +00:00
inlineMath: '$', '$'], ['\\(', '\\)'
2020-04-12 16:24:23 +00:00
},
svg: {
fontCache: 'global'
}
};
< / script >
< script type = "text/javascript" id = "MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
< / script >
< script >
function togglePopup(material_id) {
var popup = document.getElementById(material_id);
popup.classList.toggle("show");
}
< / script >
< link href = "Popups.css" rel = "stylesheet" rev = "stylesheet" type = "text/css" >
< / main >
< / body >
< / html >