53 lines
1.2 KiB
Text
53 lines
1.2 KiB
Text
|
Title: twinprimes
|
||
|
Author: Graham Nelson
|
||
|
Purpose: A single-file test web for inweb.
|
||
|
Language: C
|
||
|
Web Syntax Version: 2
|
||
|
Licence: This is a free, open-source program published under the Artistic License 2.0.
|
||
|
|
||
|
@h The conjecture.
|
||
|
It is widely believed that there are an infinite number of twin primes, that
|
||
|
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). This program finds a few small pairs of twins, by the
|
||
|
simplest method possible.
|
||
|
|
||
|
@d RANGE 100
|
||
|
|
||
|
=
|
||
|
#include <stdio.h>
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
for (int i=1; i<RANGE; i++)
|
||
|
@<Test for twin prime at i@>;
|
||
|
}
|
||
|
|
||
|
@<Test for twin prime at i@> =
|
||
|
if ((isprime(i)) && (isprime(i+2)))
|
||
|
printf("%d and %d\n", i, i+2);
|
||
|
|
||
|
@ This ought to print:
|
||
|
|
||
|
= (not code)
|
||
|
3 and 5
|
||
|
5 and 7
|
||
|
11 and 13
|
||
|
...
|
||
|
|
||
|
@h Primality.
|
||
|
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.
|
||
|
|
||
|
@d TRUE 1
|
||
|
@d FALSE 0
|
||
|
|
||
|
=
|
||
|
int isprime(int n) {
|
||
|
if (n <= 1) return FALSE;
|
||
|
for (int m = 2; m*m <= n; m++)
|
||
|
if (n % m == 0)
|
||
|
return FALSE;
|
||
|
return TRUE;
|
||
|
}
|