Triplet of three numbers: Difference between revisions
m
→{{header|EasyLang}}
(Triplet of three numbers en BASIC256) |
|||
(26 intermediate revisions by 23 users not shown) | |||
Line 5:
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V n = 6000
V p = [0B] * 6000
L(i) 2 .< Int(round(sqrt(n)))
I !p[i]
L(j) (i * 2 .< n).step(i)
p[j] = 1B
L(i) 3 .< n
I (p[i - 1] | p[i + 3] | p[i + 5])
L.continue
E
print(f:‘{i:4}: {i - 1:4} {i + 3:4} {i + 5:4}’)</syntaxhighlight>
{{out}}
<pre>
8: 7 11 13
14: 13 17 19
38: 37 41 43
68: 67 71 73
98: 97 101 103
104: 103 107 109
194: 193 197 199
224: 223 227 229
278: 277 281 283
308: 307 311 313
458: 457 461 463
614: 613 617 619
824: 823 827 829
854: 853 857 859
878: 877 881 883
1088: 1087 1091 1093
1298: 1297 1301 1303
1424: 1423 1427 1429
1448: 1447 1451 1453
1484: 1483 1487 1489
1664: 1663 1667 1669
1694: 1693 1697 1699
1784: 1783 1787 1789
1868: 1867 1871 1873
1874: 1873 1877 1879
1994: 1993 1997 1999
2084: 2083 2087 2089
2138: 2137 2141 2143
2378: 2377 2381 2383
2684: 2683 2687 2689
2708: 2707 2711 2713
2798: 2797 2801 2803
3164: 3163 3167 3169
3254: 3253 3257 3259
3458: 3457 3461 3463
3464: 3463 3467 3469
3848: 3847 3851 3853
4154: 4153 4157 4159
4514: 4513 4517 4519
4784: 4783 4787 4789
5228: 5227 5231 5233
5414: 5413 5417 5419
5438: 5437 5441 5443
5648: 5647 5651 5653
5654: 5653 5657 5659
5738: 5737 5741 5743
</pre>
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
PROC Main()
DEFINE MAX="5999"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
FOR i=3 TO MAX-5
DO
IF primes(i-1)=1 AND primes(i+3)=1 AND primes(i+5)=1 THEN
PrintF("(%I,%I,%I) ",i-1,i+3,i+5)
count==+1
FI
OD
PrintF("%E%EThere are %I triplets",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Triplet_of_three_numbers.png Screenshot from Atari 8-bit computer]
<pre>
(7,11,13) (13,17,19) (37,41,43) (67,71,73) (97,101,103) (103,107,109) (193,197,199) (223,227,229) (277,281,283)
(307,311,313) (457,461,463) (613,617,619) (823,827,829) (853,857,859) (877,881,883) (1087,1091,1093)
(1297,1301,1303) (1423,1427,1429) (1447,1451,1453) (1483,1487,1489) (1663,1667,1669) (1693,1697,1699)
(1783,1787,1789) (1867,1871,1873) (1873,1877,1879) (1993,1997,1999) (2083,2087,2089) (2137,2141,2143)
(2377,2381,2383) (2683,2687,2689) (2707,2711,2713) (2797,2801,2803) (3163,3167,3169) (3253,3257,3259)
(3457,3461,3463) (3463,3467,3469) (3847,3851,3853) (4153,4157,4159) (4513,4517,4519) (4783,4787,4789)
(5227,5231,5233) (5413,5417,5419) (5437,5441,5443) (5647,5651,5653) (5653,5657,5659) (5737,5741,5743)
There are 46 triplets
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_Io;
procedure Triplets is
Limit : constant := 5999;
Prime : array (1 .. Limit + 5) of Boolean := (others => True);
procedure Fill_Primes is
begin
Prime (1) := False;
for N in 2 .. Limit loop
if Prime (N) then
for I in 2 .. Positive'Last loop
exit when I * N not in Prime'Range;
Prime (I * N) := False;
end loop;
end if;
end loop;
end Fill_Primes;
function Is_Triplet (N : Natural) return Boolean is
begin
return
Prime (N - 1) and then
Prime (N + 3) and then
Prime (N + 5);
end Is_Triplet;
package Natural_Io is new Ada.Text_Io.Integer_Io (Natural);
use Ada.Text_Io, Natural_IO;
begin
Natural_Io.Default_Width := 5;
Fill_Primes;
for N in 2 .. Limit loop
if Is_Triplet (N) then
Put (N, Width => 4);
Put (":");
Put (N - 1);
Put (N + 3);
Put (N + 5);
New_Line;
end if;
end loop;
end Triplets;</syntaxhighlight>
{{out}}
<pre>
8: 7 11 13
14: 13 17 19
38: 37 41 43
68: 67 71 73
98: 97 101 103
104: 103 107 109
194: 193 197 199
224: 223 227 229
278: 277 281 283
308: 307 311 313
458: 457 461 463
614: 613 617 619
824: 823 827 829
854: 853 857 859
878: 877 881 883
1088: 1087 1091 1093
1298: 1297 1301 1303
1424: 1423 1427 1429
1448: 1447 1451 1453
1484: 1483 1487 1489
1664: 1663 1667 1669
1694: 1693 1697 1699
1784: 1783 1787 1789
1868: 1867 1871 1873
1874: 1873 1877 1879
1994: 1993 1997 1999
2084: 2083 2087 2089
2138: 2137 2141 2143
2378: 2377 2381 2383
2684: 2683 2687 2689
2708: 2707 2711 2713
2798: 2797 2801 2803
3164: 3163 3167 3169
3254: 3253 3257 3259
3458: 3457 3461 3463
3464: 3463 3467 3469
3848: 3847 3851 3853
4154: 4153 4157 4159
4514: 4513 4517 4519
4784: 4783 4787 4789
5228: 5227 5231 5233
5414: 5413 5417 5419
5438: 5437 5441 5443
5648: 5647 5651 5653
5654: 5653 5657 5659
5738: 5737 5741 5743
</pre>
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find numbers n where n-1, n+3 and n+5 are prime #
# sieve the primes up to
PR read "primes.incl.a68" PR
# returns a string represention of n #
OP TOSTRING = ( INT n )STRING: whole( n, 0 );
Line 22 ⟶ 213:
# 2 is clearly not a member of the required numbers, so we start at 3 #
INT n count := 0;
FOR n FROM 3 TO
IF prime[ n - 1 ] AND prime[ n + 3 ] AND prime[ n + 5 ] THEN
print( ( " (", TOSTRING n, " | ", TOSTRING ( n - 1 ), ", ", TOSTRING ( n + 3 ), ", ", TOSTRING ( n + 5 ), ")" ) );
Line 30 ⟶ 221:
OD;
print( ( newline, "Found ", TOSTRING n count, " triplets", newline ) )
END</
{{out}}
<pre>
Line 50 ⟶ 241:
=={{header|Arturo}}==
<
-> all? @[prime? x-1 prime? x+3 prime? x+5]
loop split.every: 10 lst 'a ->
print map a => [pad to :string & 5]</
{{out}}
Line 65 ⟶ 256:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f TRIPLET_OF_THREE_NUMBERS.AWK
BEGIN {
Line 92 ⟶ 283:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 147 ⟶ 338:
=={{header|BASIC}}==
<
20 DIM P(N+5)
30 FOR I=2 TO SQR(N)
Line 155 ⟶ 346:
70 IF P(I-1) OR P(I+3) OR P(I+5) GOTO 90
80 PRINT USING "####,: ####, ####, ####,";I;I-1;I+3;I+5
90 NEXT</
{{out}}
<pre style='height:50ex'> 8: 7 11 13
Line 207 ⟶ 398:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
N = 6000
dim p(N+6)
Line 227 ⟶ 418:
next i
end
</syntaxhighlight>
{{out}}
<pre>
Line 234 ⟶ 425:
=={{header|BCPL}}==
<
manifest $( limit = 6000 $)
Line 260 ⟶ 451:
writef("%I4: %I4, %I4, %I4*N", i, i-1, i+3, i+5)
freevec(prime)
$)</
{{out}}
<pre style='height:50ex'> 8: 7, 11, 13
Line 310 ⟶ 501:
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 345 ⟶ 536:
free(p);
return 0;
}</
{{out}}
<pre style='height:50ex'> 8: 7, 11, 13
14: 13, 17, 19
38: 37, 41, 43
68: 67, 71, 73
98: 97, 101, 103
104: 103, 107, 109
194: 193, 197, 199
224: 223, 227, 229
278: 277, 281, 283
308: 307, 311, 313
458: 457, 461, 463
614: 613, 617, 619
824: 823, 827, 829
854: 853, 857, 859
878: 877, 881, 883
1088: 1087, 1091, 1093
1298: 1297, 1301, 1303
1424: 1423, 1427, 1429
1448: 1447, 1451, 1453
1484: 1483, 1487, 1489
1664: 1663, 1667, 1669
1694: 1693, 1697, 1699
1784: 1783, 1787, 1789
1868: 1867, 1871, 1873
1874: 1873, 1877, 1879
1994: 1993, 1997, 1999
2084: 2083, 2087, 2089
2138: 2137, 2141, 2143
2378: 2377, 2381, 2383
2684: 2683, 2687, 2689
2708: 2707, 2711, 2713
2798: 2797, 2801, 2803
3164: 3163, 3167, 3169
3254: 3253, 3257, 3259
3458: 3457, 3461, 3463
3464: 3463, 3467, 3469
3848: 3847, 3851, 3853
4154: 4153, 4157, 4159
4514: 4513, 4517, 4519
4784: 4783, 4787, 4789
5228: 5227, 5231, 5233
5414: 5413, 5417, 5419
5438: 5437, 5441, 5443
5648: 5647, 5651, 5653
5654: 5653, 5657, 5659
5738: 5737, 5741, 5743</pre>
=={{header|C++}}==
{{trans|C}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
constexpr unsigned int LIMIT = 6000;
std::vector<bool> primes(unsigned int limit) {
std::vector<bool> p(limit + 1, true);
unsigned int root = std::sqrt(limit);
p[0] = false;
p[1] = false;
for (size_t i = 2; i <= root; i++) {
if (p[i]) {
for (size_t j = 2 * i; j <= limit; j += i) {
p[j] = false;
}
}
}
return p;
}
bool triplet(const std::vector<bool> &p, unsigned int n) {
return n >= 2 && p[n - 1] && p[n + 3] && p[n + 5];
}
int main() {
std::vector<bool> p = primes(LIMIT);
for (size_t i = 2; i < LIMIT; i++) {
if (triplet(p, i)) {
printf("%4d: %4d, %4d, %4d\n", i, i - 1, i + 3, i + 5);
}
}
return 0;
}</syntaxhighlight>
{{out}}
<pre> 8: 7, 11, 13
14: 13, 17, 19
38: 37, 41, 43
Line 396 ⟶ 675:
=={{header|C#|CSharp}}==
How about some upper limits above 6000?
<
using T3 = System.Tuple<int, int, int>; using static System.Console;
class Program { static void Main() {
Line 417 ⟶ 696:
for (int k = s, i = j << 1; k < l; k += i) f[k] = true; }
for (; j < l; lllj = llj, llj = lj, lj = j, j += 2)
if (isPrT(lllj, lj, j)) yield return new T3(lllj, lj, j); } }</
{{out}}
<pre style='height:50ex'> "N": Prime Triplet Adjacent (to previous)
Line 473 ⟶ 752:
Up to 100,000,000 there are 55,556 prime triples, of which 686 were found to be adjacent.
</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">LIMIT = 6000
isqrt = proc (s: int) returns (int)
x0: int := s/2
if x0=0 then return(s) end
x1: int := (x0 + s/x0)/2
while x1 < x0 do
x0 := x1
x1 := (x0 + s/x0)/2
end
return(x0)
end isqrt
sieve = proc (n: int) returns (array[bool])
prime: array[bool] := array[bool]$fill(2,n-1,true)
for p: int in int$from_to(2,isqrt(n)) do
if prime[p] then
for c: int in int$from_to_by(p*p,n,p) do
prime[c] := false
end
end
end
return(prime)
end sieve
triplet = proc (n: int, prime: array[bool]) returns (bool)
return(prime[n-1] & prime[n+3] & prime[n+5])
except when bounds: return(false) end
end triplet
start_up = proc ()
po: stream := stream$primary_output()
prime: array[bool] := sieve(LIMIT)
for i: int in int$from_to(2,LIMIT) do
if triplet(i, prime) then
stream$putright(po, int$unparse(i), 4)
stream$puts(po, ": ")
stream$putright(po, int$unparse(i-1), 4)
stream$puts(po, ", ")
stream$putright(po, int$unparse(i+3), 4)
stream$puts(po, ", ")
stream$putright(po, int$unparse(i+5), 4)
stream$putl(po, "")
end
end
end start_up</syntaxhighlight>
{{out}}
<pre style='height:50ex;'> 8: 7, 11, 13
14: 13, 17, 19
38: 37, 41, 43
68: 67, 71, 73
98: 97, 101, 103
104: 103, 107, 109
194: 193, 197, 199
224: 223, 227, 229
278: 277, 281, 283
308: 307, 311, 313
458: 457, 461, 463
614: 613, 617, 619
824: 823, 827, 829
854: 853, 857, 859
878: 877, 881, 883
1088: 1087, 1091, 1093
1298: 1297, 1301, 1303
1424: 1423, 1427, 1429
1448: 1447, 1451, 1453
1484: 1483, 1487, 1489
1664: 1663, 1667, 1669
1694: 1693, 1697, 1699
1784: 1783, 1787, 1789
1868: 1867, 1871, 1873
1874: 1873, 1877, 1879
1994: 1993, 1997, 1999
2084: 2083, 2087, 2089
2138: 2137, 2141, 2143
2378: 2377, 2381, 2383
2684: 2683, 2687, 2689
2708: 2707, 2711, 2713
2798: 2797, 2801, 2803
3164: 3163, 3167, 3169
3254: 3253, 3257, 3259
3458: 3457, 3461, 3463
3464: 3463, 3467, 3469
3848: 3847, 3851, 3853
4154: 4153, 4157, 4159
4514: 4513, 4517, 4519
4784: 4783, 4787, 4789
5228: 5227, 5231, 5233
5414: 5413, 5417, 5419
5438: 5437, 5441, 5443
5648: 5647, 5651, 5653
5654: 5653, 5657, 5659
5738: 5737, 5741, 5743</pre>
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 lim#:=6000
0020 DIM prime#(lim#)
0030 FOR i#:=2 TO lim# DO prime#(i#):=TRUE
0040 FOR p#:=2 TO INT(SQR(lim#)) DO
0050 FOR c#:=p#^2 TO lim# STEP p# DO prime#(c#):=FALSE
0060 ENDFOR p#
0070 FOR i#:=2 TO lim#-5 DO
0080 IF prime#(i#-1) AND prime#(i#+3) AND prime#(i#+5) THEN
0090 PRINT USING "####: ####, ####, ####":i#,i#-1,i#+3,i#+5
0100 ENDIF
0110 ENDFOR i#
0120 END</syntaxhighlight>
{{out}}
<pre style='height:50ex;'> 8: 7, 11, 13
14: 13, 17, 19
38: 37, 41, 43
68: 67, 71, 73
98: 97, 101, 103
104: 103, 107, 109
194: 193, 197, 199
224: 223, 227, 229
278: 277, 281, 283
308: 307, 311, 313
458: 457, 461, 463
614: 613, 617, 619
824: 823, 827, 829
854: 853, 857, 859
878: 877, 881, 883
1088: 1087, 1091, 1093
1298: 1297, 1301, 1303
1424: 1423, 1427, 1429
1448: 1447, 1451, 1453
1484: 1483, 1487, 1489
1664: 1663, 1667, 1669
1694: 1693, 1697, 1699
1784: 1783, 1787, 1789
1868: 1867, 1871, 1873
1874: 1873, 1877, 1879
1994: 1993, 1997, 1999
2084: 2083, 2087, 2089
2138: 2137, 2141, 2143
2378: 2377, 2381, 2383
2684: 2683, 2687, 2689
2708: 2707, 2711, 2713
2798: 2797, 2801, 2803
3164: 3163, 3167, 3169
3254: 3253, 3257, 3259
3458: 3457, 3461, 3463
3464: 3463, 3467, 3469
3848: 3847, 3851, 3853
4154: 4153, 4157, 4159
4514: 4513, 4517, 4519
4784: 4783, 4787, 4789
5228: 5227, 5231, 5233
5414: 5413, 5417, 5419
5438: 5437, 5441, 5443
5648: 5647, 5651, 5653
5654: 5653, 5657, 5659
5738: 5737, 5741, 5743</pre>
=={{header|Cowgol}}==
<
const LIMIT := 6000;
Line 511 ⟶ 946:
end if;
i := i + 1;
end loop;</
{{out}}
<pre style='height:50ex'>8: 7, 11, 13
Line 559 ⟶ 994:
5654: 5653, 5657, 5659
5738: 5737, 5741, 5743</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
<syntaxhighlight lang="Delphi">
procedure ShowTriplePrimes(Memo: TMemo);
var I: integer;
var Sieve: TPrimeSieve;
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(10000);
for I:=1 to 6000-1 do
begin
if Sieve.Flags[I-1] and
Sieve.Flags[I+3] and
Sieve.Flags[I+5] then
begin
Memo.Lines.Add(Format('%d: %d %d %d',[I,I-1,I+3,I+5]));
end;
end;
finally Sieve.Free; end;
end;
</syntaxhighlight>
{{out}}
<pre>
8: 7 11 13
14: 13 17 19
38: 37 41 43
68: 67 71 73
98: 97 101 103
104: 103 107 109
194: 193 197 199
224: 223 227 229
278: 277 281 283
308: 307 311 313
458: 457 461 463
614: 613 617 619
824: 823 827 829
854: 853 857 859
878: 877 881 883
1088: 1087 1091 1093
1298: 1297 1301 1303
1424: 1423 1427 1429
1448: 1447 1451 1453
1484: 1483 1487 1489
1664: 1663 1667 1669
1694: 1693 1697 1699
1784: 1783 1787 1789
1868: 1867 1871 1873
1874: 1873 1877 1879
1994: 1993 1997 1999
2084: 2083 2087 2089
2138: 2137 2141 2143
2378: 2377 2381 2383
2684: 2683 2687 2689
2708: 2707 2711 2713
2798: 2797 2801 2803
3164: 3163 3167 3169
3254: 3253 3257 3259
3458: 3457 3461 3463
3464: 3463 3467 3469
3848: 3847 3851 3853
4154: 4153 4157 4159
4514: 4513 4517 4519
4784: 4783 4787 4789
5228: 5227 5231 5233
5414: 5413 5417 5419
5438: 5437 5441 5443
5648: 5647 5651 5653
5654: 5653 5657 5659
5738: 5737 5741 5743
Elapsed Time: 96.852 ms.
</pre>
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
n = 6000
len p[] n + 4
for i = 2 to sqrt len p[]
if p[i] = 0
for j = i * 2 step i to len p[]
p[j] = 1
.
.
.
for i = 3 to n - 1
if p[i - 1] = 0 and p[i + 3] = 0 and p[i + 5] = 0
print i & ": " & i - 1 & " " & i + 3 & " " & i + 5
.
.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// Prime triplets: Nigel Galloway. May 18th., 2021
primes32()|>Seq.takeWhile((>)6000)|>Seq.filter(fun n->isPrime(n+4)&&isPrime(n+6))|>Seq.iter((+)1>>printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 572 ⟶ 1,107:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.statistics sequences ;
Line 582 ⟶ 1,117:
"..., %4d, [%4d], __, __, %4d, __, %4d, ...\n" printf ;
6000 4,2-gaps [ first3 [ dup 1 + ] 2dip triplet. ] each</
{{out}}
<pre style="height:14em">
Line 635 ⟶ 1,170:
=={{header|Forth}}==
{{works with|Gforth}}
<
: notprime! ( n -- ) here + 1 swap c! ;
Line 678 ⟶ 1,213:
6000 main
bye</
{{out}}
Line 735 ⟶ 1,270:
=={{header|FreeBASIC}}==
<
Dim As Integer N = 6000
Dim As Integer p(N)
Line 754 ⟶ 1,289:
Next i
Sleep
</syntaxhighlight>
<pre>
8: 7 11 13
Line 808 ⟶ 1,343:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 832 ⟶ 1,367:
}
fmt.Printf("\n%d such numbers found.\n", len(numbers))
}</
{{out}}
Line 888 ⟶ 1,423:
=={{header|J}}==
<
echo (0 _1 3 5+])"0 (triplet#]) i.6000
exit ''</
{{out}}
<pre style='height:50ex'> 8 7 11 13
Line 938 ⟶ 1,473:
5654 5653 5657 5659
5738 5737 5741 5743</pre>
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">def is_prime:
if . == 2 then true
else
2 < . and . % 2 == 1 and
(. as $in
| (($in + 1) | sqrt) as $m
| [false, 3] | until( .[0] or .[1] > $m; [$in % .[1] == 0, .[1] + 2])
| .[0]
| not)
end ;
range(3;6000) | select( all( .-1, .+3, .+5; is_prime))</syntaxhighlight>
{{out}}
<pre>
8
14
38
...
5648
5654
5738
</pre>
=={{header|Julia}}==
<
makesprimetriplet(n) = all(isprime, [n - 1, n + 3, n + 5])
println(" N Prime Triplet\n--------------------------")
foreach(n -> println(rpad(n, 6), [n - 1, n + 3, n + 5]), filter(makesprimetriplet, 2:6005))
</
<pre>
N Prime Triplet
Line 996 ⟶ 1,558:
5738 [5737, 5741, 5743]
</pre>
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
# numbers n such that n-1, n+3, and n+5 are all prime where: n < 6,000.
# # Variables:
#
integer MAX_n=6000
# # Functions:
#
# # Function _triplet(n, arr) build array of n-1, n+3, n+5
#
function _triplet {
typeset _n ; integer _n=$1
typeset _arr ; nameref _arr="$2"
_arr=( $((_n-1)) $((_n+3)) $((_n+5)) )
}
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
######
# main #
######
for ((i=2; i<MAX_n; i++)); do
typeset -a arr
_triplet ${i} arr
for ((j=0; j<${#arr[*]}; j++)); do
_isprime ${arr[j]}
(( ! $? )) && unset arr && continue 2
done
oldIFS=${IFS}
IFS=\,
print "${i}: ${arr[*]}"
IFS=${oldIFS}
unset arr
done</syntaxhighlight>
{{out}}<pre>
8: 7,11,13
14: 13,17,19
38: 37,41,43
68: 67,71,73
98: 97,101,103
104: 103,107,109
194: 193,197,199
224: 223,227,229
278: 277,281,283
308: 307,311,313
458: 457,461,463
614: 613,617,619
824: 823,827,829
854: 853,857,859
878: 877,881,883
1088: 1087,1091,1093
1298: 1297,1301,1303
1424: 1423,1427,1429
1448: 1447,1451,1453
1484: 1483,1487,1489
1664: 1663,1667,1669
1694: 1693,1697,1699
1784: 1783,1787,1789
1868: 1867,1871,1873
1874: 1873,1877,1879
1994: 1993,1997,1999
2084: 2083,2087,2089
2138: 2137,2141,2143
2378: 2377,2381,2383
2684: 2683,2687,2689
2708: 2707,2711,2713
2798: 2797,2801,2803
3164: 3163,3167,3169
3254: 3253,3257,3259
3458: 3457,3461,3463
3464: 3463,3467,3469
3848: 3847,3851,3853
4154: 4153,4157,4159
4514: 4513,4517,4519
4784: 4783,4787,4789
5228: 5227,5231,5233
5414: 5413,5417,5419
5438: 5437,5441,5443
5648: 5647,5651,5653
5654: 5653,5657,5659
5738: 5737,5741,5743</pre>
=={{header|MAD}}==
<
BOOLEAN PRIME
DIMENSION PRIME(6005)
Line 1,022 ⟶ 1,684:
VECTOR VALUES FMT = $I4,3H =,3(I5)*$
END OF PROGRAM </
{{out}}
<pre style='height:50ex'> 8 = 7 11 13
Line 1,070 ⟶ 1,732:
5654 = 5653 5657 5659
5738 = 5737 5741 5743</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Select[Range[5999], PrimeQ[# - 1] && PrimeQ[# + 3] && PrimeQ[# + 5] &]</syntaxhighlight>
{{out}}
<pre>{8, 14, 38, 68, 98, 104, 194, 224, 278, 308, 458, 614, 824, 854, 878, 1088, 1298, 1424, 1448, 1484, 1664, 1694, 1784, 1868, 1874, 1994, 2084, 2138, 2378, 2684, 2708, 2798, 3164, 3254, 3458, 3464, 3848, 4154, 4514, 4784, 5228, 5414, 5438, 5648, 5654, 5738}</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that find the number and the triple with the property */
triplets(n):=block(
L:makelist([i-1,i+3,i+5],i,1,n),
caching:length(L),
L1:[],
for i from 1 thru caching do if map(primep,L[i])=[true,true,true] then L1:endcons(append([[i]],L[i]),L1),
L1)$
/* Test case */
triplets(6000);
</syntaxhighlight>
{{out}}
<pre>
[[[8],7,11,13],[[14],13,17,19],[[38],37,41,43],[[68],67,71,73],[[98],97,101,103],[[104],103,107,109],[[194],193,197,199],[[224],223,227,229],[[278],277,281,283],[[308],307,311,313],[[458],457,461,463],[[614],613,617,619],[[824],823,827,829],[[854],853,857,859],[[878],877,881,883],[[1088],1087,1091,1093],[[1298],1297,1301,1303],[[1424],1423,1427,1429],[[1448],1447,1451,1453],[[1484],1483,1487,1489],[[1664],1663,1667,1669],[[1694],1693,1697,1699],[[1784],1783,1787,1789],[[1868],1867,1871,1873],[[1874],1873,1877,1879],[[1994],1993,1997,1999],[[2084],2083,2087,2089],[[2138],2137,2141,2143],[[2378],2377,2381,2383],[[2684],2683,2687,2689],[[2708],2707,2711,2713],[[2798],2797,2801,2803],[[3164],3163,3167,3169],[[3254],3253,3257,3259],[[3458],3457,3461,3463],[[3464],3463,3467,3469],[[3848],3847,3851,3853],[[4154],4153,4157,4159],[[4514],4513,4517,4519],[[4784],4783,4787,4789],[[5228],5227,5231,5233],[[5414],5413,5417,5419],[[5438],5437,5441,5443],[[5648],5647,5651,5653],[[5654],5653,5657,5659],[[5738],5737,5741,5743]]
</pre>
=={{header|Nim}}==
<
const
Line 1,098 ⟶ 1,783:
inc count
echo &"\nFound {count} triplets for n < {N+1}."</
{{out}}
Line 1,152 ⟶ 1,837:
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">#!/usr/bin/perl
use strict;
use warnings;
use ntheory qw( is_prime twin_primes );
is_prime($_ - 4) and printf "%5d" x 4 . "\n", $_ - 3, $_ - 4, $_, $_ + 2
for @{ twin_primes( 6000 ) };</syntaxhighlight>
{{out}}
<pre>
</pre>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">trio</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">3</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;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">trio</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;">"%d found: %V\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;">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;">5</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<small>(assumes you can add {-1,3,5} to each number in your head easily enough)</small>
Line 1,228 ⟶ 1,909:
=={{header|PL/M}}==
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,288 ⟶ 1,969:
END;
CALL EXIT;
EOF</
{{out}}
<pre style='height:50ex'>8: 7, 11, 13
Line 1,340 ⟶ 2,021:
=={{header|Python}}==
{{trans|FreeBASIC}}
<
#!/usr/bin/python3
Line 1,357 ⟶ 2,038:
else:
print(i, ': ', i-1, ' ', i+3, ' ', i+5)
</syntaxhighlight>
<pre>
Similar a la entrada de FreeBASIC.
Line 1,364 ⟶ 2,045:
=={{header|Quackery}}==
<
[ dup 2 < iff
Line 1,379 ⟶ 2,060:
else drop ]
else drop ]
echo</
{{out}}
Line 1,389 ⟶ 2,070:
A weird combination of [[Cousin primes]] and [[Twin primes]] that are siblings, but known by their neighbor.... I shall dub these Alabama primes.
<syntaxhighlight lang="raku"
{{out}}
<pre>8: (7 11 13)
Line 1,439 ⟶ 2,120:
=={{header|REXX}}==
<
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/
Line 1,445 ⟶ 2,126:
call genP hi + 5 /*build semaphore array for low primes.*/
w= 30 /*width of a prime triplet in a column.*/
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0;
$=; __= '
do j=1 for hi-1 /*look for prime triplets within range.*/
p1= j - 1; if \!.p1 then iterate /*Is P1 not prime? Then skip it. */ /* ◄■■■■■■■ a filter.*/
p3= j + 3; if \!.p3 then iterate /* " P3 " " " " " */ /* ◄■■■■■■■ a filter.*/
p5= j + 5; if \!.p5 then iterate /* " P5 " " " " " */ /* ◄■■■■■■■ a filter.*/
found= found + 1 /*bump the number of prime triplets. */
if cols<=0
ttt= commas(p1)__ commas(p3)__ commas(p5)
$= $ left( '('ttt")", w) /*add a prime triplet ──► the $ list.*/
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' strip(substr($, 2),
idx= idx + cols /*bump the index count for the output*/
end /*j*/
if $\=='' then say center(idx, 7)"│" strip(substr($, 2), 'T') /*possible show residual*/
if cols>0 then say '───────┴'center("" ,
say
say 'Found ' commas(found) title
Line 1,474 ⟶ 2,155:
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5;
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2
parse var j '' -1 _;
if j//3==0 then
do k=5 while sq.k<=j
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j;
end /*j*/; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,507 ⟶ 2,187:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 1,529 ⟶ 2,209:
see "Found " + row + " prime triplets" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,583 ⟶ 2,263:
Found 46 prime triplets
done...
</pre>
=={{header|RPL}}==
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]]:
≪ { } 1 5
7 6000 '''FOR''' j
'''IF''' j <span style="color:blue>PRIM?</span> '''THEN'''
j SWAP -
'''IF''' DUP 2 == ROT 4 == AND '''THEN'''
SWAP j 5 - + SWAP '''END'''
j '''END'''
2 '''STEP''' DROP2
≫ '<span style="color:blue>TASK</span>' STO
{{out}}
<pre>
1: { 8 14 38 68 98 104 194 224 278 308 458 614 824 854 878 1088 1298 1424 1448 1484 1664 1694 1784 1868 1874 1994 2084 2138 2378 2684 2708 2798 3164 3254 3458 3464 3848 4154 4514 4784 5228 5414 5438 5648 5654 5738 }
</pre>
Runs in 8 minutes 22 seconds on a basic HP-48G
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
primes = Prime.each(6000)
p primes.each_cons(3).filter_map{|p1, p2, p3| p1 + 1 if p1+4 == p2 && p1+6 == p3}
</syntaxhighlight>
{{out}}
<pre>[8, 14, 38, 68, 98, 104, 194, 224, 278, 308, 458, 614, 824, 854, 878, 1088, 1298, 1424, 1448, 1484, 1664, 1694, 1784, 1868, 1874, 1994, 2084, 2138, 2378, 2684, 2708, 2798, 3164, 3254, 3458, 3464, 3848, 4154, 4514, 4784, 5228, 5414, 5438, 5648, 5654, 5738]
</pre>
=={{header|Seed7}}==
<
const func boolean: isPrime (in integer: number) is func
Line 1,620 ⟶ 2,326:
end for;
writeln("\nFound " <& count <& " triplets for n < 6000.");
end func;</
{{out}}
<pre>
Line 1,676 ⟶ 2,382:
=={{header|Sidef}}==
<
{{out}}
<pre>
Line 1,685 ⟶ 2,391:
=={{header|True BASIC}}==
{{trans|FreeBASIC}}
<
LET n = 6000
Line 1,707 ⟶ 2,413:
NEXT i
END
</syntaxhighlight>
{{out}}
<pre>
Line 1,713 ⟶ 2,419:
</pre>
=={{header|V (Vlang)}}==
{{trans|Python}}
<syntaxhighlight lang="v (vlang)">import math
const n = 6000
fn main() {
mut p := []bool{len:n, init:false}
for i in 2..int(math.round(math.pow(n,.5))) {
if !p[i] {
for j:=i*2;j<n;j+=i {
p[j] = true
}
}
}
for i in 3..n {
if p[i-1] || p[i+3] || p[i+5] {
continue
}
else {
println('$i : ${i-1} ${i+3} ${i+5}')
}
}
}</syntaxhighlight>
<pre>
Similar to Go
</pre>
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var c = Int.primeSieve(6003, false)
Line 1,729 ⟶ 2,462:
}
for (n in numbers) Fmt.print("$,6d => $,6d", n, [n-1, n+3, n+5])
System.print("\nFound %(numbers.count) such numbers.")</
{{out}}
Line 1,783 ⟶ 2,516:
Found 46 such numbers.
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
int Count, N;
[ChOut(0, ^ );
Count:= 0;
for N:= 3 to 6000-1 do
if IsPrime(N-1) & IsPrime(N+3) & IsPrime(N+5) then
[IntOut(0, N-1); ChOut(0, ^ );
IntOut(0, N+3); ChOut(0, ^ );
IntOut(0, N+5); ChOut(0, ^ );
Count:= Count+1;
if rem(Count/5) then ChOut(0, 9\tab\) else CrLf(0);
];
CrLf(0);
IntOut(0, Count);
Text(0, " prime triplets found below 6000.
");
]</syntaxhighlight>
{{out}}
<pre>
7 11 13 13 17 19 37 41 43 67 71 73 97 101 103
103 107 109 193 197 199 223 227 229 277 281 283 307 311 313
457 461 463 613 617 619 823 827 829 853 857 859 877 881 883
1087 1091 1093 1297 1301 1303 1423 1427 1429 1447 1451 1453 1483 1487 1489
1663 1667 1669 1693 1697 1699 1783 1787 1789 1867 1871 1873 1873 1877 1879
1993 1997 1999 2083 2087 2089 2137 2141 2143 2377 2381 2383 2683 2687 2689
2707 2711 2713 2797 2801 2803 3163 3167 3169 3253 3257 3259 3457 3461 3463
3463 3467 3469 3847 3851 3853 4153 4157 4159 4513 4517 4519 4783 4787 4789
5227 5231 5233 5413 5417 5419 5437 5441 5443 5647 5651 5653 5653 5657 5659
5737 5741 5743
46 prime triplets found below 6000.
</pre>
=={{header|Yabasic}}==
{{trans|Python}}
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Triplet_of_three_numbers
// by Galileo, 04/2022
N = 6000
dim p(N)
for i = 2 to int(N ^ 0.5)
if not p(i) then
for j = i*2 to N step i
p(j) = 1
next
endif
next
for i = 3 to N
if not (p(i-1) or p(i+3) or p(i+5)) print i, ": ", i-1, " ", i+3, " ", i+5
next</syntaxhighlight>
{{out}}
<pre>8: 7 11 13
14: 13 17 19
38: 37 41 43
68: 67 71 73
98: 97 101 103
104: 103 107 109
194: 193 197 199
224: 223 227 229
278: 277 281 283
308: 307 311 313
458: 457 461 463
614: 613 617 619
824: 823 827 829
854: 853 857 859
878: 877 881 883
1088: 1087 1091 1093
1298: 1297 1301 1303
1424: 1423 1427 1429
1448: 1447 1451 1453
1484: 1483 1487 1489
1664: 1663 1667 1669
1694: 1693 1697 1699
1784: 1783 1787 1789
1868: 1867 1871 1873
1874: 1873 1877 1879
1994: 1993 1997 1999
2084: 2083 2087 2089
2138: 2137 2141 2143
2378: 2377 2381 2383
2684: 2683 2687 2689
2708: 2707 2711 2713
2798: 2797 2801 2803
3164: 3163 3167 3169
3254: 3253 3257 3259
3458: 3457 3461 3463
3464: 3463 3467 3469
3848: 3847 3851 3853
4154: 4153 4157 4159
4514: 4513 4517 4519
4784: 4783 4787 4789
5228: 5227 5231 5233
5414: 5413 5417 5419
5438: 5437 5441 5443
5648: 5647 5651 5653
5654: 5653 5657 5659
5738: 5737 5741 5743
---Program done, press RETURN---</pre>
|