Prime triplets
- Task
Find and show members of prime triples (p, p+2, p+6), where p < 5500
- See also
-
- The OEIS entry: A022004 - Initial members of prime triples (p, p+2, p+6)
- The Wikipedia entry: Prime triplet
- The MathWorld entry: Prime Triplet
- The RosettaCode task for just (p,p+4): Cousin primes
FreeBASIC
<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</lang>
- Output:
[ 5 7 11] [ 11 13 17] [ 17 19 23] [ 41 43 47] [ 101 103 107] [ 107 109 113] [ 191 193 197] [ 227 229 233] [ 311 313 317] [ 347 349 353] [ 461 463 467] [ 641 643 647] [ 821 823 827] [ 857 859 863] [ 881 883 887] [1091 1093 1097] [1277 1279 1283] [1301 1303 1307] [1427 1429 1433] [1481 1483 1487] [1487 1489 1493] [1607 1609 1613] [1871 1873 1877] [1997 1999 2003] [2081 2083 2087] [2237 2239 2243] [2267 2269 2273] [2657 2659 2663] [2687 2689 2693] [3251 3253 3257] [3461 3463 3467] [3527 3529 3533] [3671 3673 3677] [3917 3919 3923] [4001 4003 4007] [4127 4129 4133] [4517 4519 4523] [4637 4639 4643] [4787 4789 4793] [4931 4933 4937] [4967 4969 4973] [5231 5233 5237] [5477 5479 5483]
Go
<lang go>package main
import (
"fmt" "rcu"
)
func main() {
c := rcu.PrimeSieve(5505, false) var triples [][3]int fmt.Println("Prime triplets: p, p + 2, p + 6 where p < 5,500:") for i := 3; i < 5500; i += 2 { if !c[i] && !c[i+2] && !c[i+6] { triples = append(triples, [3]int{i, i + 2, i + 6}) } } for _, triple := range triples { var t [3]string for i := 0; i < 3; i++ { t[i] = rcu.Commatize(triple[i]) } fmt.Printf("%5s %5s %5s\n", t[0], t[1], t[2]) } fmt.Println("\nFound", len(triples), "such prime triplets.")
}</lang>
- Output:
Same as Wren entry.
Julia
<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))
</lang>
- Output:
[5, 7, 11] [11, 13, 17] [17, 19, 23] [41, 43, 47] [101, 103, 107] [107, 109, 113] [191, 193, 197] [227, 229, 233] [311, 313, 317] [347, 349, 353] [461, 463, 467] [641, 643, 647] [821, 823, 827] [857, 859, 863] [881, 883, 887] [1091, 1093, 1097] [1277, 1279, 1283] [1301, 1303, 1307] [1427, 1429, 1433] [1481, 1483, 1487] [1487, 1489, 1493] [1607, 1609, 1613] [1871, 1873, 1877] [1997, 1999, 2003] [2081, 2083, 2087] [2237, 2239, 2243] [2267, 2269, 2273] [2657, 2659, 2663] [2687, 2689, 2693] [3251, 3253, 3257] [3461, 3463, 3467] [3527, 3529, 3533] [3671, 3673, 3677] [3917, 3919, 3923] [4001, 4003, 4007] [4127, 4129, 4133] [4517, 4519, 4523] [4637, 4639, 4643] [4787, 4789, 4793] [4931, 4933, 4937] [4967, 4969, 4973] [5231, 5233, 5237] [5477, 5479, 5483]
PARI/GP
<lang parigp>for(i=1,5499,if(isprime(i)&&isprime(i+2)&&isprime(i+6),print(i," ",i+2," ",i+6)))</lang>
Phix
function pt(integer p) return is_prime(p+2) and is_prime(p+6) end function sequence res = filter(get_primes_le(5500),pt) res = apply(true,sq_add,{res,{{0,2,6}}}) res = apply(true,sprintf,{{"(%d %d %d)"},res}) printf(1,"Found %d prime triplets: %s\n",{length(res),join(shorten(res,"",2),", ")})
- Output:
Found 43 prime triplets: (5 7 11), (11 13 17), ..., (5231 5233 5237), (5477 5479 5483)
Raku
Adapted from Cousin primes
Filter
Favoring brevity over efficiency due to the small range of n, the most concise solution is: <lang perl6>say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;</lang>
- Output:
((5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483))
Infinite List
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 :
- All terms are congruent to 5 (mod 6).
<lang perl6>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);</lang>
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
REXX
<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.*/ if cols== | cols=="," then cols= 4 /* " " " " " " */ call genP hi + 6 /*build semaphore array for low primes.*/
do p=1 while @.p<hi end /*p*/; lim= p-1 /*set LIM to the Pth prime. */
w= 30 /*width of a prime triplet in a column.*/ __= ' '; @trip= ' prime triplets: p, p+2, p+6 are primes, and p < ' commas(hi) if cols>0 then say ' index │'center(@trip, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') Tprimes= 0; idx= 1 /*initialize # prime triplets & index.*/ $= /*a list of prime triplets (so far). */
do j=1 to lim /*look for prime triplets within range.*/ p2= @.j + 2; if \!.p2 then iterate /*is P2 prime? No, then skip it. */ p6= p2 + 4; if \!.p6 then iterate /* " P6 " " " " " */ Tprimes= Tprimes + 1 /*bump the number of prime triplets. */ if cols==0 then iterate /*Build the list (to be shown later)? */ @@@= commas(@.j)__ commas(p2)__ commas(p6) /*add commas & blanks to prime triplet.*/ $= $ left( '('@@@")", w) /*add a prime triplet ──► the $ list.*/ if Tprimes//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(Tprimes) @trip 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 limit /*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; s.#= @.# **2 /*number of primes so far; prime². */ /* [↓] generate more primes ≤ high.*/ do j=@.#+2 by 2 to limit /*find odd primes from here on. */ parse var j -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ if j// 3==0 then iterate /*" " " 3? */ if j// 7==0 then iterate /*" " " 7? */ /* [↑] the above 3 lines saves time.*/ do k=5 while s.k<=j /* [↓] divide by the known odd primes.*/ if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ end /*k*/ /* [↑] only process numbers ≤ √ J */ #= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ end /*j*/; return</lang>
- output when using the default inputs:
index │ prime triplets: p, p+2, p+6 are primes, and p < 5,500 ───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ (5 7 11) (11 13 17) (17 19 23) (41 43 47) 5 │ (101 103 107) (107 109 113) (191 193 197) (227 229 233) 9 │ (311 313 317) (347 349 353) (461 463 467) (641 643 647) 13 │ (821 823 827) (857 859 863) (881 883 887) (1,091 1,093 1,097) 17 │ (1,277 1,279 1,283) (1,301 1,303 1,307) (1,427 1,429 1,433) (1,481 1,483 1,487) 21 │ (1,487 1,489 1,493) (1,607 1,609 1,613) (1,871 1,873 1,877) (1,997 1,999 2,003) 25 │ (2,081 2,083 2,087) (2,237 2,239 2,243) (2,267 2,269 2,273) (2,657 2,659 2,663) 29 │ (2,687 2,689 2,693) (3,251 3,253 3,257) (3,461 3,463 3,467) (3,527 3,529 3,533) 33 │ (3,671 3,673 3,677) (3,917 3,919 3,923) (4,001 4,003 4,007) (4,127 4,129 4,133) 37 │ (4,517 4,519 4,523) (4,637 4,639 4,643) (4,787 4,789 4,793) (4,931 4,933 4,937) 41 │ (4,967 4,969 4,973) (5,231 5,233 5,237) (5,477 5,479 5,483) ───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 43 prime triplets: p, p+2, p+6 are primes, and p < 5,500
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Initial members of prime triples (p, p+2, p+6) are:" + nl see "p p+2 p+6" + nl row = 0 limit = 5500
for n = 1 to limit
if isprime(n) and isprime(n+2) and isprime(n+6) row = row + 1 see "" + n + " " + (n+2) + " " + (n+6) + nl ok
next
see "Found " + row + " primes" + nl see "done..." + nl </lang>
- Output:
working... Initial members of prime triples (p, p+2, p+6) are: p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt
var c = Int.primeSieve(5505, false) var triples = [] System.print("Prime triplets: p, p + 2, p + 6 where p < 5,500:") var i = 3 while (i < 5500) {
if (!c[i] && !c[i+2] && !c[i+6]) triples.add([i, i+2, i+6]) i = i + 2
} for (triple in triples) Fmt.print("$,6d", triple) System.print("\nFound %(triples.count) such prime triplets.")</lang>
- Output:
Prime triplets: p, p + 2, p + 6 where p < 5,500: 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1,091 1,093 1,097 1,277 1,279 1,283 1,301 1,303 1,307 1,427 1,429 1,433 1,481 1,483 1,487 1,487 1,489 1,493 1,607 1,609 1,613 1,871 1,873 1,877 1,997 1,999 2,003 2,081 2,083 2,087 2,237 2,239 2,243 2,267 2,269 2,273 2,657 2,659 2,663 2,687 2,689 2,693 3,251 3,253 3,257 3,461 3,463 3,467 3,527 3,529 3,533 3,671 3,673 3,677 3,917 3,919 3,923 4,001 4,003 4,007 4,127 4,129 4,133 4,517 4,519 4,523 4,637 4,639 4,643 4,787 4,789 4,793 4,931 4,933 4,937 4,967 4,969 4,973 5,231 5,233 5,237 5,477 5,479 5,483 Found 43 such prime triplets.