Prime triplets
- Task
Find and show members of prime triples (p, p+2, p+6), where p < 5500
- See also
-
- The OEIS entry: A022004 - Initial members of prime triples (p, p+2, p+6)
- The Wikipedia entry: Prime triplet
- The MathWorld entry: Prime Triplet
- The RosettaCode task for just (p,p+4): Cousin primes
- The RosettaCode task for other patterns of primes: Successive prime differences
11l
F is_prime(n)
I n == 2
R 1B
I n < 2 | n % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(n))).step(2)
I n % i == 0
R 0B
R 1B
print(‘ p p+2 p+6’)
V count = 0
L(n) (3.<5500).step(2)
I is_prime(n) & is_prime(n + 2) & is_prime(n + 6)
print(f:‘{n:4} {n + 2:4} {n + 6:4}’)
count++
print("\nFound "count‘ primes triplets for p < 5500.’)
- Output:
p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes triplets for p < 5500.
Action!
INCLUDE "H6:SIEVE.ACT"
PROC Main()
DEFINE MAX="5499"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
Put(125) PutE()
Sieve(primes,MAX+1)
FOR i=2 TO MAX-6
DO
IF primes(i)=1 AND primes(i+2)=1 AND (primes(i+6))=1 THEN
PrintF("(%I %I %I) ",i,i+2,i+6)
count==+1
FI
OD
PrintF("%E%EThere are %I prime triplets",count)
RETURN
- Output:
Screenshot from Atari 8-bit computer
(5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483) There are 43 prime triplets
ALGOL 68
Using code from Successive_prime_differences#ALGOL_68
BEGIN # find primes p where p+2 and p+6 are also 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 width, []INT list )VOID:
BEGIN
print( ( "[" ) );
FOR i FROM LWB list TO UPB list DO print( ( " ", whole( list[ i ], width ) ) ) 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 )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( -4, primes[ p - pattern length : p ] );
IF count MOD 6 = 0 THEN print( ( newline ) ) ELSE print( ( " " ) ) FI
FI
OD;
print( ( newline, "Found ", whole( count, 0 ), " prime sequence(s) that differ by: " ) );
print list( 0, pattern );
print( ( newline ) )
END # try differences # ;
INT max number = 5 500;
[]INT p list = prime list( max number - 1 );
print( ( "Prime triplets under ", whole( max number, 0 ), ":", newline ) );
try differences( p list, ( 2, 4 ) )
END
- Output:
Prime triplets under 5500: [ 5 7 11 ] [ 11 13 17 ] [ 17 19 23 ] [ 41 43 47 ] [ 101 103 107 ] [ 107 109 113 ] [ 191 193 197 ] [ 227 229 233 ] [ 311 313 317 ] [ 347 349 353 ] [ 461 463 467 ] [ 641 643 647 ] [ 821 823 827 ] [ 857 859 863 ] [ 881 883 887 ] [ 1091 1093 1097 ] [ 1277 1279 1283 ] [ 1301 1303 1307 ] [ 1427 1429 1433 ] [ 1481 1483 1487 ] [ 1487 1489 1493 ] [ 1607 1609 1613 ] [ 1871 1873 1877 ] [ 1997 1999 2003 ] [ 2081 2083 2087 ] [ 2237 2239 2243 ] [ 2267 2269 2273 ] [ 2657 2659 2663 ] [ 2687 2689 2693 ] [ 3251 3253 3257 ] [ 3461 3463 3467 ] [ 3527 3529 3533 ] [ 3671 3673 3677 ] [ 3917 3919 3923 ] [ 4001 4003 4007 ] [ 4127 4129 4133 ] [ 4517 4519 4523 ] [ 4637 4639 4643 ] [ 4787 4789 4793 ] [ 4931 4933 4937 ] [ 4967 4969 4973 ] [ 5231 5233 5237 ] [ 5477 5479 5483 ] Found 43 prime sequence(s) that differ by: [ 2 4 ]
Arturo
lst: select select 2..5500 => prime? 'x
-> and? [prime? x+2] [prime? x+6]
loop split.every: 5 lst 'a ->
print map a 'item [
pad join.with:", "
to [:string] @[item item+2 item+6] 17
]
- Output:
5, 7, 11 11, 13, 17 17, 19, 23 41, 43, 47 101, 103, 107 107, 109, 113 191, 193, 197 227, 229, 233 311, 313, 317 347, 349, 353 461, 463, 467 641, 643, 647 821, 823, 827 857, 859, 863 881, 883, 887 1091, 1093, 1097 1277, 1279, 1283 1301, 1303, 1307 1427, 1429, 1433 1481, 1483, 1487 1487, 1489, 1493 1607, 1609, 1613 1871, 1873, 1877 1997, 1999, 2003 2081, 2083, 2087 2237, 2239, 2243 2267, 2269, 2273 2657, 2659, 2663 2687, 2689, 2693 3251, 3253, 3257 3461, 3463, 3467 3527, 3529, 3533 3671, 3673, 3677 3917, 3919, 3923 4001, 4003, 4007 4127, 4129, 4133 4517, 4519, 4523 4637, 4639, 4643 4787, 4789, 4793 4931, 4933, 4937 4967, 4969, 4973 5231, 5233, 5237 5477, 5479, 5483
AWK
# syntax: GAWK -f PRIME_TRIPLETS.AWK
BEGIN {
start = 1
stop = 5499
for (i=start; i<=stop; i++) {
if (is_prime(i+6) && is_prime(i+2) && is_prime(i)) {
printf("%d %d %d\n",i,i+2,i+6)
count++
}
}
printf("Prime Triplets %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Prime Triplets 1-5499: 43
BASIC
BASIC256
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
for p = 3 to 5499 step 2
if not isPrime(p+6) then continue for
if not isPrime(p+2) then continue for
if not isPrime(p) then continue for
print "["; p; " "; p+2; " "; p+6; "]"
next p
end
PureBasic
Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v % 2 = 0 : ProcedureReturn #False
ElseIf v < 9 : ProcedureReturn #True
ElseIf v % 3 = 0 : ProcedureReturn #False
Else
Protected r = Round(Sqr(v), #PB_Round_Down)
Protected f = 5
While f <= r
If v % f = 0 Or v % (f + 2) = 0
ProcedureReturn #False
EndIf
f + 6
Wend
EndIf
ProcedureReturn #True
EndProcedure
OpenConsole()
For p.i = 3 To 5499 Step 2
If Not isPrime(p+6)
Continue
EndIf
If Not isPrime(p+2)
Continue
EndIf
If Not isPrime(p)
Continue
EndIf
PrintN("["+ Str(p) + " " + Str(p+2) + " " + Str(p+6) + "]")
Next p
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
Yabasic
sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 3) = 0 then return v = 3 : fi
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub
for p = 3 to 5499 step 2
if not isPrime(p+6) continue
if not isPrime(p+2) continue
if not isPrime(p) continue
print "[", p using "####", p+2 using "####", p+6 using "####", "]"
next p
end
Delphi
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
procedure ShowTriple026Prime(Memo: TMemo);
var N,Sum,Cnt: integer;
var NS,S: string;
begin
Cnt:=0;
S:='';
for N:=1 to 5500-1 do
if IsPrime(N) then
if IsPrime(N+2) and IsPrime(N+6) then
begin
Inc(Cnt);
S:=S+Format('%6d%6d%6d',[N,N+2,N+6]);
S:=S+CRLF;
end;
Memo.Lines.Add(' P P+2 P+6');
Memo.Lines.Add('------------------');
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
- Output:
P P+2 P+6 ------------------ 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Count = 43 Elapsed Time: 13.263 ms.
EasyLang
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
for i = 2 to 5499
if isprim i = 1 and isprim (i + 2) = 1 and isprim (i + 6) = 1
write "(" & i & " " & i + 2 & " " & i + 6 & ") "
.
.
- Output:
(5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483)
Factor
USING: arrays kernel lists lists.lazy math math.primes
math.primes.lists prettyprint sequences ;
lprimes ! An infinite lazy list of primes
[ dup 2 + dup 4 + 3array ] lmap-lazy ! Map primes to their triplets (e.g. 2 -> { 2 4 8 })
[ [ prime? ] all? ] lfilter ! Select triplets which contain only primes
[ first 5500 < ] lwhile ! Make the list end eventually...
[ . ] leach ! Print each item in the list
- Output:
{ 5 7 11 } { 11 13 17 } { 17 19 23 } { 41 43 47 } { 101 103 107 } { 107 109 113 } { 191 193 197 } { 227 229 233 } { 311 313 317 } { 347 349 353 } { 461 463 467 } { 641 643 647 } { 821 823 827 } { 857 859 863 } { 881 883 887 } { 1091 1093 1097 } { 1277 1279 1283 } { 1301 1303 1307 } { 1427 1429 1433 } { 1481 1483 1487 } { 1487 1489 1493 } { 1607 1609 1613 } { 1871 1873 1877 } { 1997 1999 2003 } { 2081 2083 2087 } { 2237 2239 2243 } { 2267 2269 2273 } { 2657 2659 2663 } { 2687 2689 2693 } { 3251 3253 3257 } { 3461 3463 3467 } { 3527 3529 3533 } { 3671 3673 3677 } { 3917 3919 3923 } { 4001 4003 4007 } { 4127 4129 4133 } { 4517 4519 4523 } { 4637 4639 4643 } { 4787 4789 4793 } { 4931 4933 4937 } { 4967 4969 4973 } { 5231 5233 5237 } { 5477 5479 5483 }
Fermat
for i=3,5499,2 do if Isprime(i)=1 and Isprime(i+2)=1 and Isprime(i+6)=1 then !!(i,i+2,i+6) fi od
FreeBASIC
#include "isprime.bas"
for p as uinteger = 3 to 5499 step 2
if not isprime(p+6) then continue for
if not isprime(p+2) then continue for
if not isprime(p) then continue for
print using "[#### #### ####] ";p;p+2;p+6;
next p
- Output:
[ 5 7 11] [ 11 13 17] [ 17 19 23] [ 41 43 47] [ 101 103 107] [ 107 109 113] [ 191 193 197] [ 227 229 233] [ 311 313 317] [ 347 349 353] [ 461 463 467] [ 641 643 647] [ 821 823 827] [ 857 859 863] [ 881 883 887] [1091 1093 1097] [1277 1279 1283] [1301 1303 1307] [1427 1429 1433] [1481 1483 1487] [1487 1489 1493] [1607 1609 1613] [1871 1873 1877] [1997 1999 2003] [2081 2083 2087] [2237 2239 2243] [2267 2269 2273] [2657 2659 2663] [2687 2689 2693] [3251 3253 3257] [3461 3463 3467] [3527 3529 3533] [3671 3673 3677] [3917 3919 3923] [4001 4003 4007] [4127 4129 4133] [4517 4519 4523] [4637 4639 4643] [4787 4789 4793] [4931 4933 4937] [4967 4969 4973] [5231 5233 5237] [5477 5479 5483]
FutureBasic
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
local fn PrimeTriplets( limit as NSUInteger )
NSUInteger p, i = 1
printf @"---------------------"
printf @"Index P P+2 P+6"
printf @"---------------------"
for p = 3 to limit step 2
if fn IsPrime( p+6 ) == NO then continue
if fn IsPrime( p+2 ) == NO then continue
if fn IsPrime( p ) == NO then continue
printf @"%2lu. %5lu %5lu %5lu", i, p, p+2, p+6
i++
next
end fn
fn PrimeTriplets( 5500 )
HandleEvents
- Output:
--------------------- Index P P+2 P+6 --------------------- 1. 5 7 11 2. 11 13 17 3. 17 19 23 4. 41 43 47 5. 101 103 107 6. 107 109 113 7. 191 193 197 8. 227 229 233 9. 311 313 317 10. 347 349 353 11. 461 463 467 12. 641 643 647 13. 821 823 827 14. 857 859 863 15. 881 883 887 16. 1091 1093 1097 17. 1277 1279 1283 18. 1301 1303 1307 19. 1427 1429 1433 20. 1481 1483 1487 21. 1487 1489 1493 22. 1607 1609 1613 23. 1871 1873 1877 24. 1997 1999 2003 25. 2081 2083 2087 26. 2237 2239 2243 27. 2267 2269 2273 28. 2657 2659 2663 29. 2687 2689 2693 30. 3251 3253 3257 31. 3461 3463 3467 32. 3527 3529 3533 33. 3671 3673 3677 34. 3917 3919 3923 35. 4001 4003 4007 36. 4127 4129 4133 37. 4517 4519 4523 38. 4637 4639 4643 39. 4787 4789 4793 40. 4931 4933 4937 41. 4967 4969 4973 42. 5231 5233 5237 43. 5477 5479 5483
Go
package main
import (
"fmt"
"rcu"
)
func main() {
c := rcu.PrimeSieve(5505, false)
var triples [][3]int
fmt.Println("Prime triplets: p, p + 2, p + 6 where p < 5,500:")
for i := 3; i < 5500; i += 2 {
if !c[i] && !c[i+2] && !c[i+6] {
triples = append(triples, [3]int{i, i + 2, i + 6})
}
}
for _, triple := range triples {
var t [3]string
for i := 0; i < 3; i++ {
t[i] = rcu.Commatize(triple[i])
}
fmt.Printf("%5s %5s %5s\n", t[0], t[1], t[2])
}
fmt.Println("\nFound", len(triples), "such prime triplets.")
}
- Output:
Same as Wren entry.
GW-BASIC
10 FOR A = 3 TO 5499 STEP 2
20 P = A
30 GOSUB 1000
40 IF Z = 0 THEN GOTO 500
50 P = A + 2
60 GOSUB 1000
70 IF Z = 0 THEN GOTO 500
80 P = A + 6
90 GOSUB 1000
100 IF Z = 1 THEN PRINT A,A+2,A+6
500 NEXT A
510 END
1000 Z = 1 : I = 2
1010 IF P MOD I = 0 THEN Z = 0 : RETURN
1020 I = I + 1
1030 IF I*I > P THEN RETURN
1040 GOTO 1010
J
0 2 6 +/~ ((# }:)~ 2 4 E. 2 -~/\ ]) i.&.(p:inv) 5500
Shorter, but slower:
0 2 6 +/~ I. (#: 81) E. 1 p: i. 5500
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
jq
Works with gojq, the Go implementation of jq
The implementation of `is_prime` at Erdős-primes#jq can be used here and is therefore not repeated.
The `prime_triplets` defined here generates an unbounded stream of prime triples, which is harnessed by the generic function `emit_until` defined as follows:
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
The Task:
# Output: [p,p+2,p+6] where p is prime
def prime_triplets:
def pt: .[2] == .[1] + 4 and .[1] == .[0] + 2;
def next: .[1:] + [first( range(.[2] + 2; infinite;2) | select(is_prime))];
# prime the foreach with the first triplet
foreach range(7; infinite; 2) as $i ([2,3,5]; next; select(pt) ) ;
emit_until(.[0] >= 5500; prime_triplets)
- Output:
[5,7,11] [11,13,17] [17,19,23] [41,43,47] [101,103,107] [107,109,113] [191,193,197] [227,229,233] [311,313,317] [347,349,353] [461,463,467] [641,643,647] [821,823,827] [857,859,863] [881,883,887] [1091,1093,1097] [1277,1279,1283] [1301,1303,1307] [1427,1429,1433] [1481,1483,1487] [1487,1489,1493] [1607,1609,1613] [1871,1873,1877] [1997,1999,2003] [2081,2083,2087] [2237,2239,2243] [2267,2269,2273] [2657,2659,2663] [2687,2689,2693] [3251,3253,3257] [3461,3463,3467] [3527,3529,3533] [3671,3673,3677] [3917,3919,3923] [4001,4003,4007] [4127,4129,4133] [4517,4519,4523] [4637,4639,4643] [4787,4789,4793] [4931,4933,4937] [4967,4969,4973] [5231,5233,5237] [5477,5479,5483]
Julia
using Primes
pmask = primesmask(1, 5505)
foreach(n -> println([n, n + 2, n + 6]), filter(n -> pmask[n] && pmask[n + 2] && pmask[n + 6], 1:5500))
- Output:
[5, 7, 11] [11, 13, 17] [17, 19, 23] [41, 43, 47] [101, 103, 107] [107, 109, 113] [191, 193, 197] [227, 229, 233] [311, 313, 317] [347, 349, 353] [461, 463, 467] [641, 643, 647] [821, 823, 827] [857, 859, 863] [881, 883, 887] [1091, 1093, 1097] [1277, 1279, 1283] [1301, 1303, 1307] [1427, 1429, 1433] [1481, 1483, 1487] [1487, 1489, 1493] [1607, 1609, 1613] [1871, 1873, 1877] [1997, 1999, 2003] [2081, 2083, 2087] [2237, 2239, 2243] [2267, 2269, 2273] [2657, 2659, 2663] [2687, 2689, 2693] [3251, 3253, 3257] [3461, 3463, 3467] [3527, 3529, 3533] [3671, 3673, 3677] [3917, 3919, 3923] [4001, 4003, 4007] [4127, 4129, 4133] [4517, 4519, 4523] [4637, 4639, 4643] [4787, 4789, 4793] [4931, 4933, 4937] [4967, 4969, 4973] [5231, 5233, 5237] [5477, 5479, 5483]
Lua
do -- find primes p where p+2 and p+6 are also prime
local MAX_PRIME = 5500
local pList = {}
do -- set pList to a list of primes up to MAX_PRIME
-- sieve the odd primes to n and construct the list
local p = {}
for i = 3, MAX_PRIME, 2 do p[ i ] = true end
for i = 3, math.floor( math.sqrt( MAX_PRIME ) ), 2 do
if p[ i ] then
for s = i * i, MAX_PRIME, i + i do p[ s ] = false end
end
end
pList[ 1 ] = 2
for i = 3, MAX_PRIME, 2 do
if p[ i ] then pList[ #pList + 1 ] = i end
end
end
local function fmt ( n ) return string.format( "%4d", n ) end
io.write( "Prime triplets under ", MAX_PRIME, ":", "\n" )
local tCount = 0
for i = 1, #pList - 2 do
local p1, p2, p3 = pList[ i ], pList[ i + 1 ], pList[ i + 2 ]
if p2 - p1 == 2 and p3 - p2 == 4 then
tCount = tCount + 1
io.write( "[ ", fmt( p1 ), " ", fmt( p2 ), " ", fmt( p3 ), " ]"
, ( tCount % 5 == 0 and "\n" or " " )
)
end
end
io.write( "\n", "Found ", tCount, " prime triplets\n" )
end
- Output:
Prime triplets under 5500: [ 5 7 11 ] [ 11 13 17 ] [ 17 19 23 ] [ 41 43 47 ] [ 101 103 107 ] [ 107 109 113 ] [ 191 193 197 ] [ 227 229 233 ] [ 311 313 317 ] [ 347 349 353 ] [ 461 463 467 ] [ 641 643 647 ] [ 821 823 827 ] [ 857 859 863 ] [ 881 883 887 ] [ 1091 1093 1097 ] [ 1277 1279 1283 ] [ 1301 1303 1307 ] [ 1427 1429 1433 ] [ 1481 1483 1487 ] [ 1487 1489 1493 ] [ 1607 1609 1613 ] [ 1871 1873 1877 ] [ 1997 1999 2003 ] [ 2081 2083 2087 ] [ 2237 2239 2243 ] [ 2267 2269 2273 ] [ 2657 2659 2663 ] [ 2687 2689 2693 ] [ 3251 3253 3257 ] [ 3461 3463 3467 ] [ 3527 3529 3533 ] [ 3671 3673 3677 ] [ 3917 3919 3923 ] [ 4001 4003 4007 ] [ 4127 4129 4133 ] [ 4517 4519 4523 ] [ 4637 4639 4643 ] [ 4787 4789 4793 ] [ 4931 4933 4937 ] [ 4967 4969 4973 ] [ 5231 5233 5237 ] [ 5477 5479 5483 ] Found 43 prime triplets
Mathematica / Wolfram Language
Cases[Partition[Most@NestWhileList[NextPrime, 2, # < 5500 &], 3,
1], _?(Differences[#] == {2, 4} &)] // TableForm
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
newLISP
(define (prime? n) (= 1 (length (factor n))))
(for (n 3 5500)
(let (trip (list n (+ 2 n) (+ 6 n)))
(if (for-all prime? trip) (println trip))))
Nim
import strformat
const
N = 5500 - 1
Max = N + 6
# Sieve of Erathosthenes: false (default) is composite.
var composite: array[3..Max, bool] # Ignore 2 as all primes should be odd.
var n = 3
while true:
let n2 = n * n
if n2 > Max: break
if not composite[n]:
for k in countup(n2, Max, 2 * n):
composite[k] = true
inc n, 2
template isPrime(n: int): bool = not composite[n]
echo " p p+2 p+6"
var count = 0
for n in countup(3, N, 2):
if n.isPrime and (n + 2).isPrime and (n + 6).isPrime:
echo &"{n:4} {n+2:4} {n+6:4}"
inc count
echo &"\nFound {count} primes triplets for p < {N+1}."
- Output:
p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes triplets for p < 5500.
PARI/GP
for(i=1,5499,if(isprime(i)&&isprime(i+2)&&isprime(i+6),print(i," ",i+2," ",i+6)))
Perl
#!/usr/bin/perl
use strict;
use warnings;
use ntheory qw( is_prime twin_primes );
is_prime($_ + 6) and printf "%5d" x 3 . "\n", $_, $_ + 2, $_ + 6
for @{ twin_primes( 5500 ) };
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
Phix
function pt(integer p) return is_prime(p+2) and is_prime(p+6) end function sequence res = filter(get_primes_le(5500),pt) res = apply(true,sq_add,{res,{{0,2,6}}}) res = apply(true,sprintf,{{"(%d %d %d)"},res}) printf(1,"Found %d prime triplets: %s\n",{length(res),join(shorten(res,"",2),", ")})
- Output:
Found 43 prime triplets: (5 7 11), (11 13 17), ..., (5231 5233 5237), (5477 5479 5483)
PL/0
PL/0 can only output 1 numeric value per line (and doesn't handle character data at all), so to avoid confusing output, only the first prime of each triplet is shown here.
var n, a, prime;
procedure isnprime;
var p;
begin
prime := 1;
if n < 2 then prime := 0;
if n > 2 then begin
prime := 0;
if odd( n ) then prime := 1;
p := 3;
while p * p <= n * prime do begin
if n - ( ( n / p ) * p ) = 0 then prime := 0;
p := p + 2;
end
end
end;
begin
a := 1;
while a < 5495 do begin
a := a + 2;
n := a;
call isnprime;
if prime = 1 then begin
n := a + 2;
call isnprime;
if prime = 1 then begin
n := a + 6;
call isnprime;
if prime = 1 then ! a
end
end
end
end.
- Output:
5 11 17 41 101 107 191 227 311 347 461 641 821 857 881 1091 1277 1301 1427 1481 1487 1607 1871 1997 2081 2237 2267 2657 2687 3251 3461 3527 3671 3917 4001 4127 4517 4637 4787 4931 4967 5231 5477
Python
#!/usr/bin/python
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
if __name__ == '__main__':
for p in range(3, 5499, 2):
if not isPrime(p+6):
continue
if not isPrime(p+2):
continue
if not isPrime(p):
continue
print(f'[{p} {p+2} {p+6}]')
Quackery
eratosthenes
and isprime
are defined at Sieve of Eratosthenes#Quackery.
5506 eratosthenes
[] 5500 4 - times
[ i^ isprime while
i^ 2 + isprime while
i^ 6 + isprime while
i^ dup 2 + dup 4 +
join join nested join ]
dup witheach [ echo cr ]
cr say "There are "
size echo say " prime triplets < 5500."
- Output:
[ 5 7 11 ] [ 11 13 17 ] [ 17 19 23 ] [ 41 43 47 ] [ 101 103 107 ] [ 107 109 113 ] [ 191 193 197 ] [ 227 229 233 ] [ 311 313 317 ] [ 347 349 353 ] [ 461 463 467 ] [ 641 643 647 ] [ 821 823 827 ] [ 857 859 863 ] [ 881 883 887 ] [ 1091 1093 1097 ] [ 1277 1279 1283 ] [ 1301 1303 1307 ] [ 1427 1429 1433 ] [ 1481 1483 1487 ] [ 1487 1489 1493 ] [ 1607 1609 1613 ] [ 1871 1873 1877 ] [ 1997 1999 2003 ] [ 2081 2083 2087 ] [ 2237 2239 2243 ] [ 2267 2269 2273 ] [ 2657 2659 2663 ] [ 2687 2689 2693 ] [ 3251 3253 3257 ] [ 3461 3463 3467 ] [ 3527 3529 3533 ] [ 3671 3673 3677 ] [ 3917 3919 3923 ] [ 4001 4003 4007 ] [ 4127 4129 4133 ] [ 4517 4519 4523 ] [ 4637 4639 4643 ] [ 4787 4789 4793 ] [ 4931 4933 4937 ] [ 4967 4969 4973 ] [ 5231 5233 5237 ] [ 5477 5479 5483 ] There are 43 prime triplets < 5500.
Raku
Adapted from Cousin primes
Filter
Favoring brevity over efficiency due to the small range of n, the most concise solution is:
say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;
- Output:
((5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483))
Infinite List
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 :
- All terms are congruent to 5 (mod 6).
constant @triples = (5, *+6 … *).map: -> \n { $_ if .all.is-prime given (n, n+2, n+6) }
my $count = @triples.first: :k, *.[0] >= 5500;
say .fmt('%4d') for @triples.head($count);
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
REXX
Modules: How to use
Modules: Source code
As in many other REXX entries, the needed primes are sieved by procedure Primes in Sequences.
-- 26 Mar 2025
include Settings
numeric digits 10
arg xx
if xx = '' then
xx = 5500
say 'PRIME TRIPLETS'
say version
say
call GetPrimes xx
call ShowTriplets
exit
GetPrimes:
call Time('r')
arg xx
say 'Analysis for primes up to' xx 'follows'
say
say 'Get primes...'
call Primes xx
say prim.0 'primes found'
say Format(Time('e'),,3) 'seconds'; say
return
ShowTriplets:
call Time('r')
say 'Show triplets...'
p = prim.0; p = prim.p
n = 0; a = prim.1; b = prim.2
do i = 3 to prim.0
c = prim.i
if b-a = 2 then do
if c-a = 6 then do
n = n+1
if p > 5500 then
iterate i
call Charout, Left('['a b c']',18)
if n//5 = 0 then
say
end
end
a = b; b = c
end
say
say n 'triplets found'
say Format(Time('e'),,3) 'seconds'; say
return
include Sequences
include Functions
include Abend
- Output:
PRIME TRIPLETS REXX-Regina_3.9.6(MT) 5.00 29 Apr 2024 Analysis for primes up to 5500 follows Get primes... 725 primes found 0.001 seconds Show triplets... [5 7 11] [11 13 17] [17 19 23] [41 43 47] [101 103 107] [107 109 113] [191 193 197] [227 229 233] [311 313 317] [347 349 353] [461 463 467] [641 643 647] [821 823 827] [857 859 863] [881 883 887] [1091 1093 1097] [1277 1279 1283] [1301 1303 1307] [1427 1429 1433] [1481 1483 1487] [1487 1489 1493] [1607 1609 1613] [1871 1873 1877] [1997 1999 2003] [2081 2083 2087] [2237 2239 2243] [2267 2269 2273] [2657 2659 2663] [2687 2689 2693] [3251 3253 3257] [3461 3463 3467] [3527 3529 3533] [3671 3673 3677] [3917 3919 3923] [4001 4003 4007] [4127 4129 4133] [4517 4519 4523] [4637 4639 4643] [4787 4789 4793] [4931 4933 4937] [4967 4969 4973] [5231 5233 5237] [5477 5479 5483] 43 triplets found 0.000 seconds Analysis for primes up to 1E6 follows Get primes... 78498 primes found 0.271 seconds Show triplets... 1360 triplets found 0.030 seconds Analysis for primes up to 1E7 follows Get primes... 664579 primes found 3.133 seconds Show triplets... 8384 triplets found 1.075 seconds Analysis for primes up to 1E8 follows Get primes... 5761455 primes found 38.423 seconds Show triplets... 54904 triplets found 144.341 seconds
Ring
load "stdlib.ring"
see "working..." + nl
see "Initial members of prime triples (p, p+2, p+6) are:" + nl
see "p p+2 p+6" + nl
row = 0
limit = 5500
for n = 1 to limit
if isprime(n) and isprime(n+2) and isprime(n+6)
row = row + 1
see "" + n + " " + (n+2) + " " + (n+6) + nl
ok
next
see "Found " + row + " primes" + nl
see "done..." + nl
- Output:
working... Initial members of prime triples (p, p+2, p+6) are: p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes done...
RPL
« { } 2 3 5
WHILE OVER 5500 < REPEAT
ROT DROP DUP NEXTPRIME
3 DUPN 3 →LIST
IF DUP ΔLIST { 2 4 } == THEN
5 ROLL SWAP 1 →LIST + 4 ROLLD
ELSE DROP END
END
3 DROPN
» 'TASK' STO
- Output:
1: { { 5 7 11 } { 11 13 17 } { 17 19 23 } { 41 43 47 } { 101 103 107 } { 107 109 113 } { 191 193 197 } { 227 229 233 } { 311 313 317 } { 347 349 353 } { 461 463 467 } { 641 643 647 } { 821 823 827 } { 857 859 863 } { 881 883 887 } { 1091 1093 1097 } { 1277 1279 1283 } { 1301 1303 1307 } { 1427 1429 1433 } { 1481 1483 1487 } { 1487 1489 1493 } { 1607 1609 1613 } { 1871 1873 1877 } { 1997 1999 2003 } { 2081 2083 2087 } { 2237 2239 2243 } { 2267 2269 2273 } { 2657 2659 2663 } { 2687 2689 2693 } { 3251 3253 3257 } { 3461 3463 3467 } { 3527 3529 3533 } { 3671 3673 3677 } { 3917 3919 3923 } { 4001 4003 4007 } { 4127 4129 4133 } { 4517 4519 4523 } { 4637 4639 4643 } { 4787 4789 4793 } { 4931 4933 4937 } { 4967 4969 4973 } { 5231 5233 5237 } { 5477 5479 5483 } }
Ruby
puts Prime.each(5500).each_cons(3).filter_map{|p1,p2,p3|[p1,p2,p3].join(" ") if p2-p1 == 2 && p3-p1 == 6}
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
Sidef
say "Values of p such that (p, p+2, p+6) are all prime:"
5500.primes.grep{|p| all_prime(p+2, p+6) }.say
- Output:
Values of p such that (p, p+2, p+6) are all prime: [5, 11, 17, 41, 101, 107, 191, 227, 311, 347, 461, 641, 821, 857, 881, 1091, 1277, 1301, 1427, 1481, 1487, 1607, 1871, 1997, 2081, 2237, 2267, 2657, 2687, 3251, 3461, 3527, 3671, 3917, 4001, 4127, 4517, 4637, 4787, 4931, 4967, 5231, 5477]
Tiny BASIC
LET A = 1
10 LET A = A + 2
IF A > 5499 THEN END
LET P = A
GOSUB 100
IF Z = 0 THEN GOTO 10
LET P = A + 2
GOSUB 100
IF Z = 0 THEN GOTO 10
LET P = A + 6
GOSUB 100
IF Z = 0 THEN GOTO 10
PRINT A," ",A+2," ",A+6
GOTO 10
100 REM PRIMALITY BY TRIAL DIVISION
LET Z = 1
LET I = 2
110 IF (P/I)*I = P THEN LET Z = 0
IF Z = 0 THEN RETURN
LET I = I + 1
IF I*I <= P THEN GOTO 110
RETURN
Wren
import "./math" for Int
import "./fmt" for Fmt
var c = Int.primeSieve(5505, false)
var triples = []
System.print("Prime triplets: p, p + 2, p + 6 where p < 5,500:")
var i = 3
while (i < 5500) {
if (!c[i] && !c[i+2] && !c[i+6]) triples.add([i, i+2, i+6])
i = i + 2
}
for (triple in triples) Fmt.print("$,6d", triple)
System.print("\nFound %(triples.count) such prime triplets.")
- Output:
Prime triplets: p, p + 2, p + 6 where p < 5,500: 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1,091 1,093 1,097 1,277 1,279 1,283 1,301 1,303 1,307 1,427 1,429 1,433 1,481 1,483 1,487 1,487 1,489 1,493 1,607 1,609 1,613 1,871 1,873 1,877 1,997 1,999 2,003 2,081 2,083 2,087 2,237 2,239 2,243 2,267 2,269 2,273 2,657 2,659 2,663 2,687 2,689 2,693 3,251 3,253 3,257 3,461 3,463 3,467 3,527 3,529 3,533 3,671 3,673 3,677 3,917 3,919 3,923 4,001 4,003 4,007 4,127 4,129 4,133 4,517 4,519 4,523 4,637 4,639 4,643 4,787 4,789 4,793 4,931 4,933 4,937 4,967 4,969 4,973 5,231 5,233 5,237 5,477 5,479 5,483 Found 43 such prime triplets.
XPL0
func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
int Count, P;
[ChOut(0, ^ );
Count:= 0;
P:= 3;
repeat if IsPrime(P) & IsPrime(P+2) & IsPrime(P+6) then
[IntOut(0, P); ChOut(0, ^ );
IntOut(0, P+2); ChOut(0, ^ );
IntOut(0, P+6); ChOut(0, ^ );
Count:= Count+1;
if rem(Count/5) then ChOut(0, 9\tab\) else CrLf(0);
];
P:= P+2;
until P >= 5500;
CrLf(0);
IntOut(0, Count);
Text(0, " prime triplets found below 5500.
");
]
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 43 prime triplets found below 5500.
Zig
const std = @import("std");
const ps = @cImport({
@cInclude("primesieve.h");
});
pub fn main() !void {
var t0 = try std.time.Timer.start();
const stdout = std.io.getStdOut().writer();
const limit = 5500;
var triplet_generator = try PrimeTripletGenerator.init(limit);
defer triplet_generator.deinit();
try stdout.writeAll("Primesieve prime triplets:\n");
var count: usize = 0;
while (try triplet_generator.next()) |triplet| {
count += 1;
try stdout.print("{d:4}, {d:4}, {d:4}\n", .{ triplet[0], triplet[1], triplet[2] });
}
try stdout.print("\n{d} triplets less than {d}\n\n", .{ count, limit });
try stdout.print("Processed in {}\n", .{std.fmt.fmtDuration(t0.read())});
}
const PrimeTripletGeneratorError = error{PrimeSieveError};
const PrimeTripletGenerator = struct {
stop: u64,
it: ps.primesieve_iterator,
prime1: u64,
prime2: u64,
fn init(stop: u64) !PrimeTripletGenerator {
var it: ps.primesieve_iterator = undefined;
ps.primesieve_init(&it);
var primes: [2]u64 = undefined;
for (&primes) |*prime| {
prime.* = ps.primesieve_next_prime(&it);
if (it.is_error != 0 or prime.* == ps.PRIMESIEVE_ERROR)
return PrimeTripletGeneratorError.PrimeSieveError;
}
return PrimeTripletGenerator{
.stop = stop,
.it = it,
.prime1 = primes[0],
.prime2 = primes[1],
};
}
fn deinit(self: *PrimeTripletGenerator) void {
ps.primesieve_free_iterator(&self.it);
}
fn next(self: *PrimeTripletGenerator) !?[3]u64 {
while (true) {
const prime = ps.primesieve_next_prime(&self.it);
if (self.it.is_error != 0 or prime == ps.PRIMESIEVE_ERROR)
return PrimeTripletGeneratorError.PrimeSieveError;
if (prime >= self.stop)
return null;
const prime0 = self.prime1;
self.prime1 = self.prime2;
self.prime2 = prime;
if ((self.prime1 - prime0) == 2 and (prime - prime0) == 6)
return .{ prime0, self.prime1, prime };
}
}
};
- Draft Programming Tasks
- Prime Numbers
- 11l
- Action!
- Action! Sieve of Eratosthenes
- ALGOL 68
- Arturo
- AWK
- BASIC
- BASIC256
- PureBasic
- Yabasic
- Delphi
- SysUtils,StdCtrls
- EasyLang
- Factor
- Fermat
- FreeBASIC
- FutureBasic
- Go
- Go-rcu
- GW-BASIC
- J
- Jq
- Julia
- Lua
- Mathematica
- Wolfram Language
- NewLISP
- Nim
- PARI/GP
- Perl
- Ntheory
- Phix
- PL/0
- Python
- Quackery
- Raku
- REXX
- Ring
- RPL
- Ruby
- Sidef
- Tiny BASIC
- Wren
- Wren-math
- Wren-fmt
- XPL0
- Zig
- Primesieve