Triplet of three numbers
Numbers n such that the three numbers n-1, n+3, and n+5 are all prime, where n < 6,000.
- Task
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}’)
- Output:
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
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
- Output:
Screenshot from Atari 8-bit computer
(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
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;
- Output:
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
ALGOL 68
BEGIN # find numbers n where n-1, n+3 and n+5 are prime #
# sieve the primes up to the maximum number for the task #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 6000;
# returns a string represention of n #
OP TOSTRING = ( INT n )STRING: whole( n, 0 );
# look for suitable numbers #
# 2 is clearly not a member of the required numbers, so we start at 3 #
INT n count := 0;
FOR n FROM 3 TO UPB prime - 5 DO
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 ), ")" ) );
n count +:= 1;
IF n count MOD 4 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline, "Found ", TOSTRING n count, " triplets", newline ) )
END
- Output:
(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) Found 46 triplets
Arturo
lst: select 3..6000 'x
-> all? @[prime? x-1 prime? x+3 prime? x+5]
loop split.every: 10 lst 'a ->
print map a => [pad to :string & 5]
- Output:
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
AWK
# syntax: GAWK -f TRIPLET_OF_THREE_NUMBERS.AWK
BEGIN {
start = 1
stop = 6000
print(" N N-1 N+3 N+5")
print("----- ---- ---- ----")
for (i=start; i<=stop; i++) {
if (is_prime(i-1) && is_prime(i+3) && is_prime(i+5)) {
printf("%4d: %4d %4d %4d\n",i,i-1,i+3,i+5)
count++
}
}
printf("Triplet of three numbers %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:
N N-1 N+3 N+5 ----- ---- ---- ---- 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 Triplet of three numbers 1-6000: 46
BASIC
10 DEFINT A-Z: N=6000
20 DIM P(N+5)
30 FOR I=2 TO SQR(N)
40 IF NOT P(I) THEN FOR J=I*2 TO N STEP I: P(J)=1: NEXT
50 NEXT
60 FOR I=3 TO N
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
- Output:
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 1,088: 1,087 1,091 1,093 1,298: 1,297 1,301 1,303 1,424: 1,423 1,427 1,429 1,448: 1,447 1,451 1,453 1,484: 1,483 1,487 1,489 1,664: 1,663 1,667 1,669 1,694: 1,693 1,697 1,699 1,784: 1,783 1,787 1,789 1,868: 1,867 1,871 1,873 1,874: 1,873 1,877 1,879 1,994: 1,993 1,997 1,999 2,084: 2,083 2,087 2,089 2,138: 2,137 2,141 2,143 2,378: 2,377 2,381 2,383 2,684: 2,683 2,687 2,689 2,708: 2,707 2,711 2,713 2,798: 2,797 2,801 2,803 3,164: 3,163 3,167 3,169 3,254: 3,253 3,257 3,259 3,458: 3,457 3,461 3,463 3,464: 3,463 3,467 3,469 3,848: 3,847 3,851 3,853 4,154: 4,153 4,157 4,159 4,514: 4,513 4,517 4,519 4,784: 4,783 4,787 4,789 5,228: 5,227 5,231 5,233 5,414: 5,413 5,417 5,419 5,438: 5,437 5,441 5,443 5,648: 5,647 5,651 5,653 5,654: 5,653 5,657 5,659 5,738: 5,737 5,741 5,743
BASIC256
N = 6000
dim p(N+6)
for i = 2 to sqr(N)
if not p[i] then
for j = i*2 to N step i
p[j] = 1
next j
end if
next i
for i = 3 to N
if (p[i-1] or p[i+3] or p[i+5]) then
# en BASIC256 no exite un comando CONTINUE
else
print i; ": "; i-1; " "; i+3; " "; i+5
end if
next i
end
- Output:
Similar a la entrada de FreeBASIC.
BCPL
get "libhdr"
manifest $( limit = 6000 $)
let sieve(p, n) be
$( p!0 := false
p!1 := false
for i=2 to n do p!i := true
for i=2 to n/2
if p!i
$( let j = i*2
while j <= n
$( p!j := false
j := j+i
$)
$)
$)
let triplet(p, n) = n>=2 & p!(n-1) & p!(n+3) & p!(n+5)
let start() be
$( let prime = getvec(limit)
sieve(prime, limit)
for i=2 to limit
if triplet(prime, i) do
writef("%I4: %I4, %I4, %I4*N", i, i-1, i+3, i+5)
freevec(prime)
$)
- Output:
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
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define LIMIT 6000
char *primes(unsigned int limit) {
char *p = malloc(limit + 1);
int i, j, sqr = sqrt(limit);
p[0] = p[1] = 0;
memset(p+2, 1, limit-1);
for (i=2; i<=sqr; i++)
if (p[i])
for (j=i*2; j<=limit; j+=i)
p[j] = 0;
return p;
}
int triplet(const char *p, unsigned int n) {
return n >= 2 && p[n-1] && p[n+3] && p[n+5];
}
int main() {
char *p = primes(LIMIT+5);
int i;
for (i=2; i<LIMIT; i++)
if (triplet(p, i))
printf("%4d: %4d, %4d, %4d\n", i, i-1, i+3, i+5);
free(p);
return 0;
}
- Output:
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
C++
#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;
}
- Output:
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
C#
How about some upper limits above 6000?
using System; using System.Collections.Generic; using System.Linq;
using T3 = System.Tuple<int, int, int>; using static System.Console;
class Program { static void Main() {
WriteLine(" \"N\": Prime Triplet Adjacent (to previous)\n" +
" ---- ----------------- -----------------------");
foreach(var lmt in new double[]{6e3, 1e5, 1e6, 1e7, 1e8}) {
var pr = PG.Primes((int)lmt); int l = 0, c = 0; bool a;
foreach (var t in pr) { c += (a = l == t.Item1) ? 1 : 0;
if (lmt < 1e5) WriteLine("{0,4}: {1,-18} {2}",
t.Item1 + 1, t, a ? " *" : ""); l = t.Item3; }
Console.WriteLine ("Up to {0:n0} there are {1:n0} prime triples, " +
"of which {2:n0} were found to be adjacent.", lmt, pr.Count(), c); } } }
class PG { static bool[] f; static bool isPrT(int x, int y, int z) {
if (x < 7) return false; return !f[x] && !f[y] && !f[z]; }
public static IEnumerable<T3> Primes(int l) { f = new bool[l += 6];
int j, lj, llj, lllj; j = lj = llj = lllj = 3;
for (int d = 8, s = 9; s < l; lllj = llj, llj = lj, lj = j, j += 2, s += d += 8)
if (!f[j]) { if (isPrT(lllj, lj, j)) yield return new T3(lllj, lj, j);
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); } }
- Output:
"N": Prime Triplet Adjacent (to previous) ---- ----------------- ----------------------- 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) Up to 6,000 there are 46 prime triples, of which 5 were found to be adjacent. Up to 100,000 there are 248 prime triples, of which 11 were found to be adjacent. Up to 1,000,000 there are 1,444 prime triples, of which 31 were found to be adjacent. Up to 10,000,000 there are 8,677 prime triples, of which 161 were found to be adjacent. Up to 100,000,000 there are 55,556 prime triples, of which 686 were found to be adjacent.
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
- Output:
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
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
- Output:
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
Cowgol
include "cowgol.coh";
const LIMIT := 6000;
var prime: uint8[LIMIT+5];
var i: @indexof prime;
var j: @indexof prime;
prime[0] := 0;
prime[1] := 0;
MemSet(&prime[2], 1, @bytesof prime-2);
i := 2;
while i <= @sizeof prime/2-1 loop
if prime[i] != 0 then
j := i*2;
while j <= @sizeof prime-1 loop
prime[j] := 0;
j := j+i;
end loop;
end if;
i := i+1;
end loop;
i := 2;
while i < LIMIT loop
if prime[i-1] & prime[i+3] & prime[i+5] != 0 then
print_i32(i as uint32);
print(": ");
print_i32(i as uint32-1);
print(", ");
print_i32(i as uint32+3);
print(", ");
print_i32(i as uint32+5);
print_nl();
end if;
i := i + 1;
end loop;
- Output:
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
Delphi
Uses the Delphi Prime-Generator Object
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;
- Output:
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.
EasyLang
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
.
.
F#
This task uses 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 ""
- Output:
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
Factor
USING: combinators formatting grouping kernel math math.primes
math.statistics sequences ;
: 4,2-gaps ( upto -- seq )
4 + primes-upto 3 <clumps>
[ differences { 4 2 } sequence= ] filter ;
: triplet. ( 1 n 2 3 -- )
"..., %4d, [%4d], __, __, %4d, __, %4d, ...\n" printf ;
6000 4,2-gaps [ first3 [ dup 1 + ] 2dip triplet. ] each
- Output:
..., 7, [ 8], __, __, 11, __, 13, ... ..., 13, [ 14], __, __, 17, __, 19, ... ..., 37, [ 38], __, __, 41, __, 43, ... ..., 67, [ 68], __, __, 71, __, 73, ... ..., 97, [ 98], __, __, 101, __, 103, ... ..., 103, [ 104], __, __, 107, __, 109, ... ..., 193, [ 194], __, __, 197, __, 199, ... ..., 223, [ 224], __, __, 227, __, 229, ... ..., 277, [ 278], __, __, 281, __, 283, ... ..., 307, [ 308], __, __, 311, __, 313, ... ..., 457, [ 458], __, __, 461, __, 463, ... ..., 613, [ 614], __, __, 617, __, 619, ... ..., 823, [ 824], __, __, 827, __, 829, ... ..., 853, [ 854], __, __, 857, __, 859, ... ..., 877, [ 878], __, __, 881, __, 883, ... ..., 1087, [1088], __, __, 1091, __, 1093, ... ..., 1297, [1298], __, __, 1301, __, 1303, ... ..., 1423, [1424], __, __, 1427, __, 1429, ... ..., 1447, [1448], __, __, 1451, __, 1453, ... ..., 1483, [1484], __, __, 1487, __, 1489, ... ..., 1663, [1664], __, __, 1667, __, 1669, ... ..., 1693, [1694], __, __, 1697, __, 1699, ... ..., 1783, [1784], __, __, 1787, __, 1789, ... ..., 1867, [1868], __, __, 1871, __, 1873, ... ..., 1873, [1874], __, __, 1877, __, 1879, ... ..., 1993, [1994], __, __, 1997, __, 1999, ... ..., 2083, [2084], __, __, 2087, __, 2089, ... ..., 2137, [2138], __, __, 2141, __, 2143, ... ..., 2377, [2378], __, __, 2381, __, 2383, ... ..., 2683, [2684], __, __, 2687, __, 2689, ... ..., 2707, [2708], __, __, 2711, __, 2713, ... ..., 2797, [2798], __, __, 2801, __, 2803, ... ..., 3163, [3164], __, __, 3167, __, 3169, ... ..., 3253, [3254], __, __, 3257, __, 3259, ... ..., 3457, [3458], __, __, 3461, __, 3463, ... ..., 3463, [3464], __, __, 3467, __, 3469, ... ..., 3847, [3848], __, __, 3851, __, 3853, ... ..., 4153, [4154], __, __, 4157, __, 4159, ... ..., 4513, [4514], __, __, 4517, __, 4519, ... ..., 4783, [4784], __, __, 4787, __, 4789, ... ..., 5227, [5228], __, __, 5231, __, 5233, ... ..., 5413, [5414], __, __, 5417, __, 5419, ... ..., 5437, [5438], __, __, 5441, __, 5443, ... ..., 5647, [5648], __, __, 5651, __, 5653, ... ..., 5653, [5654], __, __, 5657, __, 5659, ... ..., 5737, [5738], __, __, 5741, __, 5743, ...
Forth
: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
: prime_sieve { n -- }
here n erase
0 notprime!
1 notprime!
n 4 > if
n 4 do i notprime! 2 +loop
then
3
begin
dup dup * n <
while
dup prime? if
n over dup * do
i notprime!
dup 2* +loop
then
2 +
repeat
drop ;
: main { n -- }
." N N-1 N+3 N+5" cr
n prime_sieve
0
n 1 do
i 1- prime? if
i 3 + prime? if
i 5 + prime? if
i 4 .r ." :"
i 1- 6 .r
i 3 + 6 .r
i 5 + 6 .r cr
1+
then
then
then
loop
cr ." Count: " . cr ;
6000 main
bye
- Output:
N N-1 N+3 N+5 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 Count: 46
FreeBASIC
Dim As Integer N = 6000
Dim As Integer p(N)
For i As Integer = 2 To Sqr(N)
If Not p(i) Then
For j As Integer = i * 2 To N Step i
p(j) = 1
Next j
End If
Next i
For i As Integer = 3 To N
If (p(i-1) Or p(i+3) Or p(i+5)) Then
Continue For
Else
Print Using "####,: ####, ####, ####,"; i; i-1; i+3; i+5
End If
Next i
Sleep
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 1,088: 1,087 1,091 1,093 1,298: 1,297 1,301 1,303 1,424: 1,423 1,427 1,429 1,448: 1,447 1,451 1,453 1,484: 1,483 1,487 1,489 1,664: 1,663 1,667 1,669 1,694: 1,693 1,697 1,699 1,784: 1,783 1,787 1,789 1,868: 1,867 1,871 1,873 1,874: 1,873 1,877 1,879 1,994: 1,993 1,997 1,999 2,084: 2,083 2,087 2,089 2,138: 2,137 2,141 2,143 2,378: 2,377 2,381 2,383 2,684: 2,683 2,687 2,689 2,708: 2,707 2,711 2,713 2,798: 2,797 2,801 2,803 3,164: 3,163 3,167 3,169 3,254: 3,253 3,257 3,259 3,458: 3,457 3,461 3,463 3,464: 3,463 3,467 3,469 3,848: 3,847 3,851 3,853 4,154: 4,153 4,157 4,159 4,514: 4,513 4,517 4,519 4,784: 4,783 4,787 4,789 5,228: 5,227 5,231 5,233 5,414: 5,413 5,417 5,419 5,438: 5,437 5,441 5,443 5,648: 5,647 5,651 5,653 5,654: 5,653 5,657 5,659 5,738: 5,737 5,741 5,743
Go
package main
import (
"fmt"
"rcu"
)
func main() {
c := rcu.PrimeSieve(6003, false)
var numbers []int
fmt.Println("Numbers n < 6000 where: n - 1, n + 3, n + 5 are all primes:")
for n := 4; n < 6000; n += 2 {
if !c[n-1] && !c[n+3] && !c[n+5] {
numbers = append(numbers, n)
}
}
for _, n := range numbers {
fmt.Printf("%6s => ", rcu.Commatize(n))
for _, p := range []int{n - 1, n + 3, n + 5} {
fmt.Printf("%6s ", rcu.Commatize(p))
}
fmt.Println()
}
fmt.Printf("\n%d such numbers found.\n", len(numbers))
}
- Output:
Numbers n < 6000 where: n - 1, n + 3, n + 5 are all primes: 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 1,088 => 1,087 1,091 1,093 1,298 => 1,297 1,301 1,303 1,424 => 1,423 1,427 1,429 1,448 => 1,447 1,451 1,453 1,484 => 1,483 1,487 1,489 1,664 => 1,663 1,667 1,669 1,694 => 1,693 1,697 1,699 1,784 => 1,783 1,787 1,789 1,868 => 1,867 1,871 1,873 1,874 => 1,873 1,877 1,879 1,994 => 1,993 1,997 1,999 2,084 => 2,083 2,087 2,089 2,138 => 2,137 2,141 2,143 2,378 => 2,377 2,381 2,383 2,684 => 2,683 2,687 2,689 2,708 => 2,707 2,711 2,713 2,798 => 2,797 2,801 2,803 3,164 => 3,163 3,167 3,169 3,254 => 3,253 3,257 3,259 3,458 => 3,457 3,461 3,463 3,464 => 3,463 3,467 3,469 3,848 => 3,847 3,851 3,853 4,154 => 4,153 4,157 4,159 4,514 => 4,513 4,517 4,519 4,784 => 4,783 4,787 4,789 5,228 => 5,227 5,231 5,233 5,414 => 5,413 5,417 5,419 5,438 => 5,437 5,441 5,443 5,648 => 5,647 5,651 5,653 5,654 => 5,653 5,657 5,659 5,738 => 5,737 5,741 5,743 46 such numbers found.
J
triplet=: (1 *./@p: _1 3 5+])"0
echo (0 _1 3 5+])"0 (triplet#]) i.6000
exit ''
- Output:
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
jq
Works with gojq, the Go implementation of 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))
- Output:
8 14 38 ... 5648 5654 5738
Julia
using Primes
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))
- Output:
N Prime Triplet -------------------------- 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, 196, 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]
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
- Output:
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
MAD
NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(6005)
LIMIT = 6000
PRIME(0) = 0B
PRIME(1) = 0B
THROUGH SET, FOR I=2, 1, I.G.LIMIT+5
SET PRIME(I) = 1B
LAST = SQRT.(LIMIT+5)
THROUGH SIEVE, FOR I=2, 1, I.G.LAST
WHENEVER PRIME(I)
THROUGH UNSET, FOR J=I*2, I, J.G.LIMIT+5
UNSET PRIME(J) = 0B
END OF CONDITIONAL
SIEVE CONTINUE
THROUGH TEST, FOR I=2, 1, I.G.LIMIT
WHENEVER PRIME(I-1).AND.PRIME(I+3).AND.PRIME(I+5)
PRINT FORMAT FMT, I, I-1, I+3, I+5
END OF CONDITIONAL
TEST CONTINUE
VECTOR VALUES FMT = $I4,3H =,3(I5)*$
END OF PROGRAM
- Output:
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
Mathematica /Wolfram Language
Select[Range[5999], PrimeQ[# - 1] && PrimeQ[# + 3] && PrimeQ[# + 5] &]
- Output:
{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}
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);
- Output:
[[[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]]
Nim
import strformat
const
N = 5999
Max = 6003 # 5998 + 5.
# 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 " n n-1 n+3 n+5"
var count = 0
for n in countup(4, N, 2):
if (n - 1).isPrime and (n + 3).isPrime and (n + 5).isPrime:
echo &"{n:4}: {n-1:4} {n+3:4} {n+5:4}"
inc count
echo &"\nFound {count} triplets for n < {N+1}."
- Output:
n n-1 n+3 n+5 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 Found 46 triplets for n < 6000.
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 ) };
- Output:
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
Phix
function trio(integer n) return sum(apply({n-1,n+3,n+5},is_prime))=3 end function sequence res = filter(tagset(6000),trio) printf(1,"%d found: %V\n",{length(res),shorten(res,"",5)})
- Output:
(assumes you can add {-1,3,5} to each number in your head easily enough)
46 found: {8,14,38,68,98,"...",5414,5438,5648,5654,5738}
PL/M
100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9, S); END PRINT;
DECLARE LIMIT LITERALLY '6000';
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (6) BYTE INITIAL ('.....$');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P-1;
C = N MOD 10 + '0';
N = N/10;
IF N>0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
SIEVE: PROCEDURE (PX, N);
DECLARE (PX, N, P BASED PX) ADDRESS;
DECLARE (I, J) ADDRESS;
P(0) = 0;
P(1) = 0;
DO I=2 TO N;
P(I) = 1;
END;
DO I=2 TO N/2;
IF P(I) THEN
DO J=I*2 TO N BY I;
P(J) = 0;
END;
END;
END SIEVE;
IS$TRIPLE: PROCEDURE (PX, N) BYTE;
DECLARE (PX, N, P BASED PX) ADDRESS;
IF N < 2 THEN RETURN 0;
RETURN P(N-1) AND P(N+3) AND P(N+5);
END IS$TRIPLE;
PRINT$TRIPLE: PROCEDURE (N);
DECLARE COMMA DATA (', $');
DECLARE N ADDRESS;
CALL PRINT$NUMBER(N);
CALL PRINT(.': $');
CALL PRINT$NUMBER(N-1);
CALL PRINT(.COMMA);
CALL PRINT$NUMBER(N+3);
CALL PRINT(.COMMA);
CALL PRINT$NUMBER(N+5);
CALL PRINT(.(13,10,'$'));
END PRINT$TRIPLE;
DECLARE I ADDRESS;
CALL SIEVE(.MEMORY, LIMIT+5);
DO I=2 TO LIMIT;
IF IS$TRIPLE(.MEMORY, I) THEN CALL PRINT$TRIPLE(I);
END;
CALL EXIT;
EOF
- Output:
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
Python
#!/usr/bin/python3
N = 6000
p = [None] * 6000 #inicializamos la lista
for i in range(2, round(pow(N,0.5))):
if not p[i]:
for j in range(i*2, N, i):
p[j] = 1
for i in range(3, N):
if (p[i-1] or p[i+3] or p[i+5]):
continue
else:
print(i, ': ', i-1, ' ', i+3, ' ', i+5)
Similar a la entrada de FreeBASIC.
Quackery
[ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
[ dup 2 < iff
[ drop false ] done
dup 1 - ! 1+
swap mod 0 = ] is prime ( n --> b )
[] 3000 times
[ i^ 2 *
dup 1 - prime iff
[ dup 3 + prime iff
[ dup 5 + prime iff
join else drop ]
else drop ]
else drop ]
echo
- Output:
[ 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 ]
Raku
A weird combination of Cousin primes and Twin primes that are siblings, but known by their neighbor.... I shall dub these Alabama primes.
say "{.[0]+1}: ",$_ for grep *.all.is-prime, ^6000 .race.map: { $_-1, $_+3, $_+5 };
- Output:
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)
REXX
/*REXX pgm finds prime triplets: n-1, n+3, n+5 are primes, and n < some specified #.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 4 /* " " " " " " */
call genP hi + 5 /*build semaphore array for low primes.*/
w= 30 /*width of a prime triplet in a column.*/
title= ' prime triplets: n-1, n+3, n+5 are primes, and n < ' commas(hi)
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0; idx= 1 /*initialize # prime triplets & index.*/
$=; __= ' ' /*a list of prime triplets (so far). */
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 then iterate /*Build the list (to be shown later)? */
ttt= commas(p1)__ commas(p3)__ commas(p5) /*add commas & blanks to prime triplet.*/
$= $ 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), "T"); $= /*show what we have so far.*/
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("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(found) title
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0; parse arg hip /*placeholders for primes (semaphores).*/
@.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; sq.#= @.# ** 2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 for max(0, hip%2-@.#%2-1) /*find odd primes from here on.*/
parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit).*/
if j//3==0 then iterate; if j//7==0 then iterate /*" " " 3? Is J ÷ by 7? */
do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/
if j//@.k==0 then iterate j /*Is J÷@.k ? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return
- output when using the default inputs:
index │ prime triplets: n-1, n+3, n+5 are primes, and n < 6,000 ───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ (7 11 13) (13 17 19) (37 41 43) (67 71 73) 5 │ (97 101 103) (103 107 109) (193 197 199) (223 227 229) 9 │ (277 281 283) (307 311 313) (457 461 463) (613 617 619) 13 │ (823 827 829) (853 857 859) (877 881 883) (1,087 1,091 1,093) 17 │ (1,297 1,301 1,303) (1,423 1,427 1,429) (1,447 1,451 1,453) (1,483 1,487 1,489) 21 │ (1,663 1,667 1,669) (1,693 1,697 1,699) (1,783 1,787 1,789) (1,867 1,871 1,873) 25 │ (1,873 1,877 1,879) (1,993 1,997 1,999) (2,083 2,087 2,089) (2,137 2,141 2,143) 29 │ (2,377 2,381 2,383) (2,683 2,687 2,689) (2,707 2,711 2,713) (2,797 2,801 2,803) 33 │ (3,163 3,167 3,169) (3,253 3,257 3,259) (3,457 3,461 3,463) (3,463 3,467 3,469) 37 │ (3,847 3,851 3,853) (4,153 4,157 4,159) (4,513 4,517 4,519) (4,783 4,787 4,789) 41 │ (5,227 5,231 5,233) (5,413 5,417 5,419) (5,437 5,441 5,443) (5,647 5,651 5,653) 45 │ (5,653 5,657 5,659) (5,737 5,741 5,743) ───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 46 prime triplets: n-1, n+3, n+5 are primes, and n < 6,000
Ring
load "stdlib.ring"
see "working..." + nl
see "n prime triplet" + nl
see "----------------" + nl
row = 0
limit = 6000
for n = 2 to limit-2
bool1 = isprime(n-1)
bool2 = isprime(n+3)
bool3 = isprime(n+5)
bool = bool1 and bool2 and bool3
if bool
row = row + 1
see "" + n + ": (" + (n-1) + " " + (n+3) + " " + (n+5) + ")" + nl
ok
next
see "Found " + row + " prime triplets" + nl
see "done..." + nl
- Output:
working... n prime triplet ---------------- 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) Found 46 prime triplets done...
RPL
PRIM?
is defined at Primality by trial division:
≪ { } 1 5 7 6000 FOR j IF j PRIM? THEN j SWAP - IF DUP 2 == ROT 4 == AND THEN SWAP j 5 - + SWAP END j END 2 STEP DROP2 ≫ 'TASK' STO
- Output:
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 }
Runs in 8 minutes 22 seconds on a basic HP-48G
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}
- Output:
[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]
Seed7
$ include "seed7_05.s7i";
const func boolean: isPrime (in integer: number) is func
result
var boolean: prime is FALSE;
local
var integer: upTo is 0;
var integer: testNum is 3;
begin
if number = 2 then
prime := TRUE;
elsif odd(number) and number > 2 then
upTo := sqrt(number);
while number rem testNum <> 0 and testNum <= upTo do
testNum +:= 2;
end while;
prime := testNum > upTo;
end if;
end func;
const proc: main is func
local
var integer: n is 0;
var integer: count is 0;
begin
writeln(" n n-3 n+3 n+5");
writeln("--------------------");
for n range 2 to 5998 step 2 do
if isPrime(n - 1) and isPrime(n + 3) and isPrime(n + 5) then
writeln(n lpad 4 <& ":" <& n - 1 lpad 5 <& n + 3 lpad 5 <& n + 5 lpad 5);
incr(count);
end if;
end for;
writeln("\nFound " <& count <& " triplets for n < 6000.");
end func;
- Output:
n n-3 n+3 n+5 -------------------- 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 Found 46 triplets for n < 6000.
Sidef
^6000 -> grep {|n| [-1, 3, 5].all {|k| n + k -> is_prime } }.say
- Output:
[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]
True BASIC
LET n = 6000
DIM p(0)
MAT REDIM p(n)
FOR i = 2 TO SQR(n)
IF (NOT p(i) <> 0) THEN
FOR j = i*2 TO n STEP i
LET p(j) = 1
NEXT j
END IF
NEXT i
FOR i = 3 TO n
IF (p(i-1) <> 0 OR p(i+3) <> 0 OR p(i+5) <> 0) THEN
! en TB no exite un comando CONTINUE
ELSE
PRINT USING "####: #### #### ####": i, i-1, i+3, i+5
END IF
NEXT i
END
- Output:
Similar a la entrada de FreeBASIC.
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}')
}
}
}
Similar to Go
Wren
import "./math" for Int
import "./fmt" for Fmt
var c = Int.primeSieve(6003, false)
var numbers = []
System.print("Numbers n < 6000 where: n - 1, n + 3, n + 5 are all primes:")
var n = 4
while (n < 6000) {
if (!c[n-1] && !c[n+3] && !c[n+5]) numbers.add(n)
n = n + 2
}
for (n in numbers) Fmt.print("$,6d => $,6d", n, [n-1, n+3, n+5])
System.print("\nFound %(numbers.count) such numbers.")
- Output:
Numbers n < 6000 where: n - 1, n + 3, n + 5 are all primes: 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 1,088 => 1,087 1,091 1,093 1,298 => 1,297 1,301 1,303 1,424 => 1,423 1,427 1,429 1,448 => 1,447 1,451 1,453 1,484 => 1,483 1,487 1,489 1,664 => 1,663 1,667 1,669 1,694 => 1,693 1,697 1,699 1,784 => 1,783 1,787 1,789 1,868 => 1,867 1,871 1,873 1,874 => 1,873 1,877 1,879 1,994 => 1,993 1,997 1,999 2,084 => 2,083 2,087 2,089 2,138 => 2,137 2,141 2,143 2,378 => 2,377 2,381 2,383 2,684 => 2,683 2,687 2,689 2,708 => 2,707 2,711 2,713 2,798 => 2,797 2,801 2,803 3,164 => 3,163 3,167 3,169 3,254 => 3,253 3,257 3,259 3,458 => 3,457 3,461 3,463 3,464 => 3,463 3,467 3,469 3,848 => 3,847 3,851 3,853 4,154 => 4,153 4,157 4,159 4,514 => 4,513 4,517 4,519 4,784 => 4,783 4,787 4,789 5,228 => 5,227 5,231 5,233 5,414 => 5,413 5,417 5,419 5,438 => 5,437 5,441 5,443 5,648 => 5,647 5,651 5,653 5,654 => 5,653 5,657 5,659 5,738 => 5,737 5,741 5,743 Found 46 such numbers.
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.
");
]
- Output:
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.
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
- Output:
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---