Prime triplets: Difference between revisions

m
m (added the Prime Numbers category.)
m (→‎{{header|Wren}}: Minor tidy)
 
(41 intermediate revisions by 22 users not shown)
Line 10:
:*   The MathWorld entry:   [https://mathworld.wolfram.com/PrimeTriplet.html Prime Triplet]
:*   The RosettaCode task for just (p,p+4):   [[Cousin primes]]
:*   The RosettaCode task for other patterns of primes:   [[Successive prime differences]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="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.’)</syntaxhighlight>
 
{{out}}
<pre>
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.
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Prime_triplets.png Screenshot from Atari 8-bit computer]
<pre>
(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
</pre>
 
=={{header|ALGOL 68}}==
Using code from [[Successive_prime_differences#ALGOL_68]]
<syntaxhighlight lang="algol68">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</syntaxhighlight>
{{out}}
<pre>
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 ]
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">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
]</syntaxhighlight>
 
{{out}}
 
<pre> 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</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="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)
}
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="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</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="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()</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="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</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="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;
 
</syntaxhighlight>
{{out}}
<pre>
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.
 
</pre>
 
 
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
<pre style="height:14em">
{ 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 }
</pre>
 
=={{header|Fermat}}==
<syntaxhighlight lang="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</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}<pre>
[ 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]
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="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
</syntaxhighlight>
{{output}}
<pre>
---------------------
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
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 39 ⟶ 657:
}
fmt.Println("\nFound", len(triples), "such prime triplets.")
}</langsyntaxhighlight>
 
{{out}}
<pre>
Same as Wren entry.
</pre>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">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</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j">0 2 6 +/~ ((# }:)~ 2 4 E. 2 -~/\ ]) i.&.(p:inv) 5500</syntaxhighlight>
Shorter, but slower:
<syntaxhighlight lang="j">0 2 6 +/~ I. (#: 81) E. 1 p: i. 5500</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The implementation of `is_prime` at [[Erd%C5%91s-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:
<syntaxhighlight lang="jq">
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</syntaxhighlight>
The Task:
<syntaxhighlight lang="jq"># 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) </syntaxhighlight>
{{out}}
<pre>
[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]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="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))
</langsyntaxhighlight>{{out}}
<pre>
[5, 7, 11]
Line 96 ⟶ 849:
[5231, 5233, 5237]
[5477, 5479, 5483]
</pre>
=={{header|Lua}}==
<syntaxhighlight lang="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
</syntaxhighlight>
{{out}}
<pre>
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</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Cases[Partition[Most@NestWhileList[NextPrime, 2, # < 5500 &], 3,
1], _?(Differences[#] == {2, 4} &)] // TableForm</syntaxhighlight>
 
{{out}}<pre>
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
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="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}."</syntaxhighlight>
 
{{out}}
<pre> 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.</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">for(i=1,5499,if(isprime(i)&&isprime(i+2)&&isprime(i+6),print(i," ",i+2," ",i+6)))</syntaxhighlight>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="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 ) };</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">pt</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5500</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">)</span>
Line 105 ⟶ 1,092:
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"(%d %d %d)"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d prime triplets: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Found 43 prime triplets: (5 7 11), (11 13 17), ..., (5231 5233 5237), (5477 5479 5483)
</pre>
 
 
=={{header|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.
<syntaxhighlight lang="pascal">
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.
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="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}]')</syntaxhighlight>
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="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."</syntaxhighlight>
{{out}}
 
<pre>[ 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.</pre>
 
=={{header|Raku}}==
Line 115 ⟶ 1,269:
===Filter===
Favoring brevity over efficiency due to the small range of n, the most concise solution is:
<syntaxhighlight lang="raku" perl6line>say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;</langsyntaxhighlight>
{{out}}
<pre>
Line 123 ⟶ 1,277:
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).
<syntaxhighlight lang="raku" perl6line>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);</langsyntaxhighlight>
{{out}}
<pre>
Line 175 ⟶ 1,329:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds prime triplets: P, P+2, P+6 are primes, and P < some specified N*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 5500 /*Not specified? Then use the default.*/
Line 222 ⟶ 1,376:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 244 ⟶ 1,398:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 261 ⟶ 1,415:
see "Found " + row + " primes" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 314 ⟶ 1,468:
 
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« { } 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
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
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 } }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">puts Prime.each(5500).each_cons(3).filter_map{|p1,p2,p3|[p1,p2,p3].join(" ") if p2-p1 == 2 && p3-p1 == 6}
</syntaxhighlight>
{{out}}
<pre>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
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">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</syntaxhighlight>
{{out}}
<pre>
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]
</pre>
 
=={{header|Tiny BASIC}}==
<syntaxhighlight lang="tinybasic"> 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</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var c = Int.primeSieve(5505, false)
Line 330 ⟶ 1,583:
}
for (triple in triples) Fmt.print("$,6d", triple)
System.print("\nFound %(triples.count) such prime triplets.")</langsyntaxhighlight>
 
{{out}}
Line 380 ⟶ 1,633:
 
Found 43 such prime triplets.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="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.
");
]</syntaxhighlight>
 
{{out}}
<pre>
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.
</pre>
9,476

edits