Double Twin Primes
Double Twin Primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition
Let (p1,p2) and (p3,p4) be twin primes where p3 - p2 = 4.
Such primes called Double Twin Primes
Example
[5,7,11,13]
Task
Find and show here all Double Twin Primes under 1000.
ALGOL 68
Reusing a lot of code from the Successive prime differences task.
BEGIN # find some sequences of primes where the gaps between the elements #
# are 2, 4, 2 - i.e., n, n+2, n+6 and n+8 are all prime #
# reurns a list of primes up to n #
PROC prime list = ( INT n )[]INT:
BEGIN
# sieve the primes to n #
INT no = 0, yes = 1;
[ 1 : n ]INT p;
p[ 1 ] := no; p[ 2 ] := yes;
FOR i FROM 3 BY 2 TO n DO p[ i ] := yes OD;
FOR i FROM 4 BY 2 TO n DO p[ i ] := no OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
IF p[ i ] = yes THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := no OD FI
OD;
# replace the sieve with a list #
INT p pos := 0;
FOR i TO n DO IF p[ i ] = yes THEN p[ p pos +:= 1 ] := i FI OD;
p[ 1 : p pos ]
END # prime list # ;
# prints the elements of list #
PROC print list = ( []INT list )VOID:
BEGIN
print( ( "[" ) );
FOR i FROM LWB list TO UPB list DO print( ( " ", whole( list[ i ], 0 ) ) ) OD;
print( ( " ]" ) )
END # print list # ;
# attempts to find patterns in the differences of primes and prints the results #
PROC try differences = ( []INT primes, []INT pattern, INT max prime )VOID:
BEGIN
INT pattern length = ( UPB pattern - LWB pattern ) + 1;
[ 1 : pattern length + 1 ]INT first; FOR i TO UPB first DO first[ i ] := 0 OD;
[ 1 : pattern length + 1 ]INT last; FOR i TO UPB last DO last[ i ] := 0 OD;
INT count := 0;
FOR p FROM LWB primes + pattern length TO UPB primes DO
BOOL matched := TRUE;
INT e pos := LWB pattern;
FOR e FROM p - pattern length TO p - 1
WHILE matched := primes[ e + 1 ] - primes[ e ] = pattern[ e pos ]
DO
e pos +:= 1
OD;
IF matched THEN
# found a matching sequence #
count +:= 1;
print list( primes[ p - pattern length : p ] );
print( ( newline ) )
FI
OD;
print( ( "Found "
, whole( count, 0 )
, " prime sequences with differences: "
)
);
print list( pattern );
print( ( " up to ", whole( max prime, 0 ) ) );
print( ( newline ) )
END # try differences # ;
INT max number = 1 000;
[]INT p list = prime list( max number );
try differences( p list, ( 2, 4, 2 ), max number )
END
- Output:
[ 5 7 11 13 ] [ 11 13 17 19 ] [ 101 103 107 109 ] [ 191 193 197 199 ] [ 821 823 827 829 ] Found 5 prime sequences with differences: [ 2 4 2 ] up to 1000
C
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
if (n < 2) return false;
if (n%2 == 0) return n == 2;
if (n%3 == 0) return n == 3;
int d = 5;
while (d*d <= n) {
if (n%d == 0) return false;
d += 2;
if (n%d == 0) return false;
d += 4;
}
return true;
}
int main() {
printf("Double twin primes under 1,000:\n");
for (int i = 3; i < 992; i+=2) {
if (isPrime(i) && isPrime(i+2) && isPrime(i+6) && isPrime(i+8)) {
printf("%4d %4d %4d %4d\n", i, i+2, i+6, i+8);
}
}
return 0;
}
- Output:
Double twin primes under 1,000: 5 7 11 13 11 13 17 19 101 103 107 109 191 193 197 199 821 823 827 829
FreeBASIC
#include "isprime.bas"
Dim As Uinteger num = 3
Do
If isPrime(num) Then
If isPrime(num+2) Then
If isPrime(num+6) Then
If isPrime(num+8) Then Print num; " "; num+2; " "; num+6; " "; num+8
End If
End If
End If
num += 2
Loop Until num >= 1000-8
Sleep
- Output:
5 7 11 13 11 13 17 19 101 103 107 109 191 193 197 199 821 823 827 829
Go
package main
import (
"fmt"
"rcu"
)
func main() {
p := rcu.Primes(1000)
fmt.Println("Double twin primes under 1,000:")
for i := 1; i < len(p)-3; i++ {
if p[i+1]-p[i] == 2 && p[i+2]-p[i+1] == 4 && p[i+3]-p[i+2] == 2 {
fmt.Printf("%4d\n", p[i:i+4])
}
}
}
- Output:
Double twin primes under 1,000: [ 5 7 11 13] [ 11 13 17 19] [ 101 103 107 109] [ 191 193 197 199] [ 821 823 827 829]
Raku
Cousin twin primes:
sub dt { $^p, $p+2, $p+6, $p+8 }
.&dt.say for (^1000).grep: { all .&dt».is-prime };
- Output:
(5 7 11 13) (11 13 17 19) (101 103 107 109) (191 193 197 199) (821 823 827 829)
Ring
see "works..." + nl
primes = []
limit = 1000
for n =1 to limit
if isPrime(n)
add(primes,n)
ok
next
lenPrimes = len(primes)-3
for m = 1 to lenPrimes
if isPrime(primes[m]) and isPrime(primes[m+1]) and
isPrime(primes[m+2]) and isPrime(primes[m+3])
if (primes[m+1] - primes[m] = 2) and (primes[m+2] - primes[m+1] = 4) and
(primes[m+3] - primes[m+2] = 2)
see " " + primes[m]+ " " + primes[m+1] + " " +
primes[m+2] + " " + primes[m+3] + nl
ok
ok
next
see "done..." + nl
func isPrime num
if (num <= 1) return 0 ok
if (num % 2 = 0 and num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1
- Output:
works... 5 7 11 13 11 13 17 19 101 103 107 109 191 193 197 199 821 823 827 829 done...
Wren
import "./math" for Int
import "./fmt" for Fmt
var p = Int.primeSieve(1000)
System.print("Double twin primes under 1,000:")
for (i in 1...p.count-3) {
if (p[i+1] - p[i] == 2 && p[i+2] - p[i+1] == 4 && p[i+3] - p[i+2] == 2) {
Fmt.aprint(p[i..i+3], 4, 0, "")
}
}
- Output:
Double twin primes under 1,000: 5 7 11 13 11 13 17 19 101 103 107 109 191 193 197 199 821 823 827 829
XPL0
func IsPrime(N); \Return 'true' if odd N is prime
int N, D;
[for D:= 3 to sqrt(N) do
[if rem(N/D) = 0 then return false;
D:= D+1;
];
return true;
];
int N;
[N:= 3;
repeat if IsPrime(N) then
if IsPrime(N+2) then
if IsPrime(N+6) then
if IsPrime(N+8) then
[IntOut(0, N); ChOut(0, ^ );
IntOut(0, N+2); ChOut(0, ^ );
IntOut(0, N+6); ChOut(0, ^ );
IntOut(0, N+8); CrLf(0);
];
N:= N+2;
until N >= 1000-8;
]
- Output:
5 7 11 13 11 13 17 19 101 103 107 109 191 193 197 199 821 823 827 829