Prime triplets: Difference between revisions
(Added Sidef) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16: | Line 16: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">F is_prime(n) |
||
I n == 2 |
I n == 2 |
||
R 1B |
R 1B |
||
Line 33: | Line 33: | ||
count++ |
count++ |
||
print("\nFound "count‘ primes triplets for p < 5500.’)</ |
print("\nFound "count‘ primes triplets for p < 5500.’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 87: | Line 87: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Sieve of Eratosthenes}} |
{{libheader|Action! Sieve of Eratosthenes}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT" |
||
PROC Main() |
PROC Main() |
||
Line 104: | Line 104: | ||
OD |
OD |
||
PrintF("%E%EThere are %I prime triplets",count) |
PrintF("%E%EThere are %I prime triplets",count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Prime_triplets.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Prime_triplets.png Screenshot from Atari 8-bit computer] |
||
Line 120: | Line 120: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Using code from [[Successive_prime_differences#ALGOL_68]] |
Using code from [[Successive_prime_differences#ALGOL_68]] |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find primes p where p+2 and p+6 are also prime # |
||
# reurns a list of primes up to n # |
# reurns a list of primes up to n # |
||
PROC prime list = ( INT n )[]INT: |
PROC prime list = ( INT n )[]INT: |
||
Line 175: | Line 175: | ||
print( ( "Prime triplets under ", whole( max number, 0 ), ":", newline ) ); |
print( ( "Prime triplets under ", whole( max number, 0 ), ":", newline ) ); |
||
try differences( p list, ( 2, 4 ) ) |
try differences( p list, ( 2, 4 ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 192: | Line 192: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">lst: select select 2..5500 => prime? 'x |
||
-> and? [prime? x+2] [prime? x+6] |
-> and? [prime? x+2] [prime? x+6] |
||
Line 199: | Line 199: | ||
pad join.with:", " |
pad join.with:", " |
||
to [:string] @[item item+2 item+6] 17 |
to [:string] @[item item+2 item+6] 17 |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 214: | Line 214: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f PRIME_TRIPLETS.AWK |
# syntax: GAWK -f PRIME_TRIPLETS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 239: | Line 239: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 291: | Line 291: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang="basic256">function isPrime(v) |
||
if v < 2 then return False |
if v < 2 then return False |
||
if v mod 2 = 0 then return v = 2 |
if v mod 2 = 0 then return v = 2 |
||
Line 308: | Line 308: | ||
print "["; p; " "; p+2; " "; p+6; "]" |
print "["; p; " "; p+2; " "; p+6; "]" |
||
next p |
next p |
||
end</ |
end</syntaxhighlight> |
||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
< |
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i) |
||
If v <= 1 : ProcedureReturn #False |
If v <= 1 : ProcedureReturn #False |
||
ElseIf v < 4 : ProcedureReturn #True |
ElseIf v < 4 : ProcedureReturn #True |
||
Line 344: | Line 344: | ||
Next p |
Next p |
||
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() |
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() |
||
CloseConsole()</ |
CloseConsole()</syntaxhighlight> |
||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang="yabasic"> |
||
sub isPrime(v) |
sub isPrime(v) |
||
if v < 2 then return False : fi |
if v < 2 then return False : fi |
||
Line 365: | Line 365: | ||
print "[", p using "####", p+2 using "####", p+6 using "####", "]" |
print "[", p using "####", p+2 using "####", p+6 using "####", "]" |
||
next p |
next p |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.98}} |
{{works with|Factor|0.98}} |
||
< |
<syntaxhighlight lang="factor">USING: arrays kernel lists lists.lazy math math.primes |
||
math.primes.lists prettyprint sequences ; |
math.primes.lists prettyprint sequences ; |
||
Line 377: | Line 377: | ||
[ [ prime? ] all? ] lfilter ! Select triplets which contain only primes |
[ [ prime? ] all? ] lfilter ! Select triplets which contain only primes |
||
[ first 5500 < ] lwhile ! Make the list end eventually... |
[ first 5500 < ] lwhile ! Make the list end eventually... |
||
[ . ] leach ! Print each item in the list</ |
[ . ] leach ! Print each item in the list</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em"> |
<pre style="height:14em"> |
||
Line 426: | Line 426: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">for i=3,5499,2 do if Isprime(i)=1 and Isprime(i+2)=1 and Isprime(i+6)=1 then !!(i,i+2,i+6) fi od</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">#include "isprime.bas" |
||
for p as uinteger = 3 to 5499 step 2 |
for p as uinteger = 3 to 5499 step 2 |
||
if not isprime(p+6) then continue for |
if not isprime(p+6) then continue for |
||
Line 435: | Line 435: | ||
if not isprime(p) then continue for |
if not isprime(p) then continue for |
||
print using "[#### #### ####] ";p;p+2;p+6; |
print using "[#### #### ####] ";p;p+2;p+6; |
||
next p</ |
next p</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
[ 5 7 11] [ 11 13 17] [ 17 19 23] [ 41 43 47] [ 101 103 107] [ 107 109 113] [ 191 193 197] [ 227 229 233] [ 311 313 317] [ 347 349 353] [ 461 463 467] [ 641 643 647] [ 821 823 827] [ 857 859 863] [ 881 883 887] [1091 1093 1097] [1277 1279 1283] [1301 1303 1307] [1427 1429 1433] [1481 1483 1487] [1487 1489 1493] [1607 1609 1613] [1871 1873 1877] [1997 1999 2003] [2081 2083 2087] [2237 2239 2243] [2267 2269 2273] [2657 2659 2663] [2687 2689 2693] [3251 3253 3257] [3461 3463 3467] [3527 3529 3533] [3671 3673 3677] [3917 3919 3923] [4001 4003 4007] [4127 4129 4133] [4517 4519 4523] [4637 4639 4643] [4787 4789 4793] [4931 4933 4937] [4967 4969 4973] [5231 5233 5237] [5477 5479 5483] |
[ 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] |
||
Line 443: | Line 443: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 467: | Line 467: | ||
} |
} |
||
fmt.Println("\nFound", len(triples), "such prime triplets.") |
fmt.Println("\nFound", len(triples), "such prime triplets.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 475: | Line 475: | ||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic">10 FOR A = 3 TO 5499 STEP 2 |
||
20 P = A |
20 P = A |
||
30 GOSUB 1000 |
30 GOSUB 1000 |
||
Line 491: | Line 491: | ||
1020 I = I + 1 |
1020 I = I + 1 |
||
1030 IF I*I > P THEN RETURN |
1030 IF I*I > P THEN RETURN |
||
1040 GOTO 1010</ |
1040 GOTO 1010</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 500: | Line 500: | ||
The `prime_triplets` defined here generates an unbounded stream of prime triples, which is harnessed by the generic function `emit_until` defined as follows: |
The `prime_triplets` defined here generates an unbounded stream of prime triples, which is harnessed by the generic function `emit_until` defined as follows: |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</ |
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</syntaxhighlight> |
||
The Task: |
The Task: |
||
< |
<syntaxhighlight lang="jq"># Output: [p,p+2,p+6] where p is prime |
||
def prime_triplets: |
def prime_triplets: |
||
def pt: .[2] == .[1] + 4 and .[1] == .[0] + 2; |
def pt: .[2] == .[1] + 4 and .[1] == .[0] + 2; |
||
Line 510: | Line 510: | ||
foreach range(7; infinite; 2) as $i ([2,3,5]; next; select(pt) ) ; |
foreach range(7; infinite; 2) as $i ([2,3,5]; next; select(pt) ) ; |
||
emit_until(.[0] >= 5500; prime_triplets) </ |
emit_until(.[0] >= 5500; prime_triplets) </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 559: | Line 559: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
pmask = primesmask(1, 5505) |
pmask = primesmask(1, 5505) |
||
foreach(n -> println([n, n + 2, n + 6]), filter(n -> pmask[n] && pmask[n + 2] && pmask[n + 6], 1:5500)) |
foreach(n -> println([n, n + 2, n + 6]), filter(n -> pmask[n] && pmask[n + 2] && pmask[n + 6], 1:5500)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
[5, 7, 11] |
[5, 7, 11] |
||
Line 610: | Line 610: | ||
</pre> |
</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Cases[Partition[Most@NestWhileList[NextPrime, 2, # < 5500 &], 3, |
||
1], _?(Differences[#] == {2, 4} &)] // TableForm</ |
1], _?(Differences[#] == {2, 4} &)] // TableForm</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
Line 660: | Line 660: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
const |
const |
||
Line 686: | Line 686: | ||
inc count |
inc count |
||
echo &"\nFound {count} primes triplets for p < {N+1}."</ |
echo &"\nFound {count} primes triplets for p < {N+1}."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 737: | Line 737: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">for(i=1,5499,if(isprime(i)&&isprime(i+2)&&isprime(i+6),print(i," ",i+2," ",i+6)))</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 748: | Line 748: | ||
is_prime($_ + 6) and printf "%5d" x 3 . "\n", $_, $_ + 2, $_ + 6 |
is_prime($_ + 6) and printf "%5d" x 3 . "\n", $_, $_ + 2, $_ + 6 |
||
for @{ twin_primes( 5500 ) };</ |
for @{ twin_primes( 5500 ) };</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 797: | Line 797: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">pt</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">pt</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5500</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">)</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5500</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">)</span> |
||
Line 803: | Line 803: | ||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"(%d %d %d)"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"(%d %d %d)"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d prime triplets: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d prime triplets: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 811: | Line 811: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/python |
||
def isPrime(n): |
def isPrime(n): |
||
Line 827: | Line 827: | ||
if not isPrime(p): |
if not isPrime(p): |
||
continue |
continue |
||
print(f'[{p} {p+2} {p+6}]')</ |
print(f'[{p} {p+2} {p+6}]')</syntaxhighlight> |
||
Line 834: | Line 834: | ||
===Filter=== |
===Filter=== |
||
Favoring brevity over efficiency due to the small range of n, the most concise solution is: |
Favoring brevity over efficiency due to the small range of n, the most concise solution is: |
||
<lang |
<syntaxhighlight lang="raku" line>say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 842: | Line 842: | ||
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 : |
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). |
:All terms are congruent to 5 (mod 6). |
||
<lang |
<syntaxhighlight lang="raku" line>constant @triples = (5, *+6 … *).map: -> \n { $_ if .all.is-prime given (n, n+2, n+6) } |
||
my $count = @triples.first: :k, *.[0] >= 5500; |
my $count = @triples.first: :k, *.[0] >= 5500; |
||
say .fmt('%4d') for @triples.head($count);</ |
say .fmt('%4d') for @triples.head($count);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 894: | Line 894: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight 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.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 5500 /*Not specified? Then use the default.*/ |
if hi=='' | hi=="," then hi= 5500 /*Not specified? Then use the default.*/ |
||
Line 941: | Line 941: | ||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</ |
end /*j*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 963: | Line 963: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 980: | Line 980: | ||
see "Found " + row + " primes" + nl |
see "Found " + row + " primes" + nl |
||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,035: | Line 1,035: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">say "Values of p such that (p, p+2, p+6) are all prime:" |
||
5500.primes.grep{|p| all_prime(p+2, p+6) }.say</ |
5500.primes.grep{|p| all_prime(p+2, p+6) }.say</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,044: | Line 1,044: | ||
=={{header|Tiny BASIC}}== |
=={{header|Tiny BASIC}}== |
||
< |
<syntaxhighlight lang="tinybasic"> LET A = 1 |
||
10 LET A = A + 2 |
10 LET A = A + 2 |
||
IF A > 5499 THEN END |
IF A > 5499 THEN END |
||
Line 1,065: | Line 1,065: | ||
LET I = I + 1 |
LET I = I + 1 |
||
IF I*I <= P THEN GOTO 110 |
IF I*I <= P THEN GOTO 110 |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,082: | Line 1,082: | ||
} |
} |
||
for (triple in triples) Fmt.print("$,6d", triple) |
for (triple in triples) Fmt.print("$,6d", triple) |
||
System.print("\nFound %(triples.count) such prime triplets.")</ |
System.print("\nFound %(triples.count) such prime triplets.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,135: | Line 1,135: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number |
||
int N, I; |
int N, I; |
||
[if N <= 1 then return false; |
[if N <= 1 then return false; |
||
Line 1,160: | Line 1,160: | ||
Text(0, " prime triplets found below 5500. |
Text(0, " prime triplets found below 5500. |
||
"); |
"); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 11:15, 28 August 2022
- Task
Find and show members of prime triples (p, p+2, p+6), where p < 5500
- See also
-
- The OEIS entry: A022004 - Initial members of prime triples (p, p+2, p+6)
- The Wikipedia entry: Prime triplet
- The MathWorld entry: Prime Triplet
- The RosettaCode task for just (p,p+4): Cousin primes
- The RosettaCode task for other patterns of primes: Successive prime differences
11l
F is_prime(n)
I n == 2
R 1B
I n < 2 | n % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(n))).step(2)
I n % i == 0
R 0B
R 1B
print(‘ p p+2 p+6’)
V count = 0
L(n) (3.<5500).step(2)
I is_prime(n) & is_prime(n + 2) & is_prime(n + 6)
print(f:‘{n:4} {n + 2:4} {n + 6:4}’)
count++
print("\nFound "count‘ primes triplets for p < 5500.’)
- Output:
p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes triplets for p < 5500.
Action!
INCLUDE "H6:SIEVE.ACT"
PROC Main()
DEFINE MAX="5499"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
Put(125) PutE()
Sieve(primes,MAX+1)
FOR i=2 TO MAX-6
DO
IF primes(i)=1 AND primes(i+2)=1 AND (primes(i+6))=1 THEN
PrintF("(%I %I %I) ",i,i+2,i+6)
count==+1
FI
OD
PrintF("%E%EThere are %I prime triplets",count)
RETURN
- Output:
Screenshot from Atari 8-bit computer
(5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483) There are 43 prime triplets
ALGOL 68
Using code from Successive_prime_differences#ALGOL_68
BEGIN # find primes p where p+2 and p+6 are also prime #
# reurns a list of primes up to n #
PROC prime list = ( INT n )[]INT:
BEGIN
# sieve the primes to n #
INT no = 0, yes = 1;
[ 1 : n ]INT p;
p[ 1 ] := no; p[ 2 ] := yes;
FOR i FROM 3 BY 2 TO n DO p[ i ] := yes OD;
FOR i FROM 4 BY 2 TO n DO p[ i ] := no OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
IF p[ i ] = yes THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := no OD FI
OD;
# replace the sieve with a list #
INT p pos := 0;
FOR i TO n DO IF p[ i ] = yes THEN p[ p pos +:= 1 ] := i FI OD;
p[ 1 : p pos ]
END # prime list # ;
# prints the elements of list #
PROC print list = ( INT width, []INT list )VOID:
BEGIN
print( ( "[" ) );
FOR i FROM LWB list TO UPB list DO print( ( " ", whole( list[ i ], width ) ) ) OD;
print( ( " ]" ) )
END # print list # ;
# attempts to find patterns in the differences of primes and prints the results #
PROC try differences = ( []INT primes, []INT pattern )VOID:
BEGIN
INT pattern length = ( UPB pattern - LWB pattern ) + 1;
[ 1 : pattern length + 1 ]INT first; FOR i TO UPB first DO first[ i ] := 0 OD;
[ 1 : pattern length + 1 ]INT last; FOR i TO UPB last DO last[ i ] := 0 OD;
INT count := 0;
FOR p FROM LWB primes + pattern length TO UPB primes DO
BOOL matched := TRUE;
INT e pos := LWB pattern;
FOR e FROM p - pattern length TO p - 1
WHILE matched := primes[ e + 1 ] - primes[ e ] = pattern[ e pos ]
DO
e pos +:= 1
OD;
IF matched THEN
# found a matching sequence #
count +:= 1;
print list( -4, primes[ p - pattern length : p ] );
IF count MOD 6 = 0 THEN print( ( newline ) ) ELSE print( ( " " ) ) FI
FI
OD;
print( ( newline, "Found ", whole( count, 0 ), " prime sequence(s) that differ by: " ) );
print list( 0, pattern );
print( ( newline ) )
END # try differences # ;
INT max number = 5 500;
[]INT p list = prime list( max number - 1 );
print( ( "Prime triplets under ", whole( max number, 0 ), ":", newline ) );
try differences( p list, ( 2, 4 ) )
END
- Output:
Prime triplets under 5500: [ 5 7 11 ] [ 11 13 17 ] [ 17 19 23 ] [ 41 43 47 ] [ 101 103 107 ] [ 107 109 113 ] [ 191 193 197 ] [ 227 229 233 ] [ 311 313 317 ] [ 347 349 353 ] [ 461 463 467 ] [ 641 643 647 ] [ 821 823 827 ] [ 857 859 863 ] [ 881 883 887 ] [ 1091 1093 1097 ] [ 1277 1279 1283 ] [ 1301 1303 1307 ] [ 1427 1429 1433 ] [ 1481 1483 1487 ] [ 1487 1489 1493 ] [ 1607 1609 1613 ] [ 1871 1873 1877 ] [ 1997 1999 2003 ] [ 2081 2083 2087 ] [ 2237 2239 2243 ] [ 2267 2269 2273 ] [ 2657 2659 2663 ] [ 2687 2689 2693 ] [ 3251 3253 3257 ] [ 3461 3463 3467 ] [ 3527 3529 3533 ] [ 3671 3673 3677 ] [ 3917 3919 3923 ] [ 4001 4003 4007 ] [ 4127 4129 4133 ] [ 4517 4519 4523 ] [ 4637 4639 4643 ] [ 4787 4789 4793 ] [ 4931 4933 4937 ] [ 4967 4969 4973 ] [ 5231 5233 5237 ] [ 5477 5479 5483 ] Found 43 prime sequence(s) that differ by: [ 2 4 ]
Arturo
lst: select select 2..5500 => prime? 'x
-> and? [prime? x+2] [prime? x+6]
loop split.every: 5 lst 'a ->
print map a 'item [
pad join.with:", "
to [:string] @[item item+2 item+6] 17
]
- Output:
5, 7, 11 11, 13, 17 17, 19, 23 41, 43, 47 101, 103, 107 107, 109, 113 191, 193, 197 227, 229, 233 311, 313, 317 347, 349, 353 461, 463, 467 641, 643, 647 821, 823, 827 857, 859, 863 881, 883, 887 1091, 1093, 1097 1277, 1279, 1283 1301, 1303, 1307 1427, 1429, 1433 1481, 1483, 1487 1487, 1489, 1493 1607, 1609, 1613 1871, 1873, 1877 1997, 1999, 2003 2081, 2083, 2087 2237, 2239, 2243 2267, 2269, 2273 2657, 2659, 2663 2687, 2689, 2693 3251, 3253, 3257 3461, 3463, 3467 3527, 3529, 3533 3671, 3673, 3677 3917, 3919, 3923 4001, 4003, 4007 4127, 4129, 4133 4517, 4519, 4523 4637, 4639, 4643 4787, 4789, 4793 4931, 4933, 4937 4967, 4969, 4973 5231, 5233, 5237 5477, 5479, 5483
AWK
# syntax: GAWK -f PRIME_TRIPLETS.AWK
BEGIN {
start = 1
stop = 5499
for (i=start; i<=stop; i++) {
if (is_prime(i+6) && is_prime(i+2) && is_prime(i)) {
printf("%d %d %d\n",i,i+2,i+6)
count++
}
}
printf("Prime Triplets %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Prime Triplets 1-5499: 43
BASIC
BASIC256
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
for p = 3 to 5499 step 2
if not isPrime(p+6) then continue for
if not isPrime(p+2) then continue for
if not isPrime(p) then continue for
print "["; p; " "; p+2; " "; p+6; "]"
next p
end
PureBasic
Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v % 2 = 0 : ProcedureReturn #False
ElseIf v < 9 : ProcedureReturn #True
ElseIf v % 3 = 0 : ProcedureReturn #False
Else
Protected r = Round(Sqr(v), #PB_Round_Down)
Protected f = 5
While f <= r
If v % f = 0 Or v % (f + 2) = 0
ProcedureReturn #False
EndIf
f + 6
Wend
EndIf
ProcedureReturn #True
EndProcedure
OpenConsole()
For p.i = 3 To 5499 Step 2
If Not isPrime(p+6)
Continue
EndIf
If Not isPrime(p+2)
Continue
EndIf
If Not isPrime(p)
Continue
EndIf
PrintN("["+ Str(p) + " " + Str(p+2) + " " + Str(p+6) + "]")
Next p
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
Yabasic
sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 3) = 0 then return v = 3 : fi
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub
for p = 3 to 5499 step 2
if not isPrime(p+6) continue
if not isPrime(p+2) continue
if not isPrime(p) continue
print "[", p using "####", p+2 using "####", p+6 using "####", "]"
next p
end
Factor
USING: arrays kernel lists lists.lazy math math.primes
math.primes.lists prettyprint sequences ;
lprimes ! An infinite lazy list of primes
[ dup 2 + dup 4 + 3array ] lmap-lazy ! Map primes to their triplets (e.g. 2 -> { 2 4 8 })
[ [ prime? ] all? ] lfilter ! Select triplets which contain only primes
[ first 5500 < ] lwhile ! Make the list end eventually...
[ . ] leach ! Print each item in the list
- Output:
{ 5 7 11 } { 11 13 17 } { 17 19 23 } { 41 43 47 } { 101 103 107 } { 107 109 113 } { 191 193 197 } { 227 229 233 } { 311 313 317 } { 347 349 353 } { 461 463 467 } { 641 643 647 } { 821 823 827 } { 857 859 863 } { 881 883 887 } { 1091 1093 1097 } { 1277 1279 1283 } { 1301 1303 1307 } { 1427 1429 1433 } { 1481 1483 1487 } { 1487 1489 1493 } { 1607 1609 1613 } { 1871 1873 1877 } { 1997 1999 2003 } { 2081 2083 2087 } { 2237 2239 2243 } { 2267 2269 2273 } { 2657 2659 2663 } { 2687 2689 2693 } { 3251 3253 3257 } { 3461 3463 3467 } { 3527 3529 3533 } { 3671 3673 3677 } { 3917 3919 3923 } { 4001 4003 4007 } { 4127 4129 4133 } { 4517 4519 4523 } { 4637 4639 4643 } { 4787 4789 4793 } { 4931 4933 4937 } { 4967 4969 4973 } { 5231 5233 5237 } { 5477 5479 5483 }
Fermat
for i=3,5499,2 do if Isprime(i)=1 and Isprime(i+2)=1 and Isprime(i+6)=1 then !!(i,i+2,i+6) fi od
FreeBASIC
#include "isprime.bas"
for p as uinteger = 3 to 5499 step 2
if not isprime(p+6) then continue for
if not isprime(p+2) then continue for
if not isprime(p) then continue for
print using "[#### #### ####] ";p;p+2;p+6;
next p
- Output:
[ 5 7 11] [ 11 13 17] [ 17 19 23] [ 41 43 47] [ 101 103 107] [ 107 109 113] [ 191 193 197] [ 227 229 233] [ 311 313 317] [ 347 349 353] [ 461 463 467] [ 641 643 647] [ 821 823 827] [ 857 859 863] [ 881 883 887] [1091 1093 1097] [1277 1279 1283] [1301 1303 1307] [1427 1429 1433] [1481 1483 1487] [1487 1489 1493] [1607 1609 1613] [1871 1873 1877] [1997 1999 2003] [2081 2083 2087] [2237 2239 2243] [2267 2269 2273] [2657 2659 2663] [2687 2689 2693] [3251 3253 3257] [3461 3463 3467] [3527 3529 3533] [3671 3673 3677] [3917 3919 3923] [4001 4003 4007] [4127 4129 4133] [4517 4519 4523] [4637 4639 4643] [4787 4789 4793] [4931 4933 4937] [4967 4969 4973] [5231 5233 5237] [5477 5479 5483]
Go
package main
import (
"fmt"
"rcu"
)
func main() {
c := rcu.PrimeSieve(5505, false)
var triples [][3]int
fmt.Println("Prime triplets: p, p + 2, p + 6 where p < 5,500:")
for i := 3; i < 5500; i += 2 {
if !c[i] && !c[i+2] && !c[i+6] {
triples = append(triples, [3]int{i, i + 2, i + 6})
}
}
for _, triple := range triples {
var t [3]string
for i := 0; i < 3; i++ {
t[i] = rcu.Commatize(triple[i])
}
fmt.Printf("%5s %5s %5s\n", t[0], t[1], t[2])
}
fmt.Println("\nFound", len(triples), "such prime triplets.")
}
- Output:
Same as Wren entry.
GW-BASIC
10 FOR A = 3 TO 5499 STEP 2
20 P = A
30 GOSUB 1000
40 IF Z = 0 THEN GOTO 500
50 P = A + 2
60 GOSUB 1000
70 IF Z = 0 THEN GOTO 500
80 P = A + 6
90 GOSUB 1000
100 IF Z = 1 THEN PRINT A,A+2,A+6
500 NEXT A
510 END
1000 Z = 1 : I = 2
1010 IF P MOD I = 0 THEN Z = 0 : RETURN
1020 I = I + 1
1030 IF I*I > P THEN RETURN
1040 GOTO 1010
jq
Works with gojq, the Go implementation of jq
The implementation of `is_prime` at Erdős-primes#jq can be used here and is therefore not repeated.
The `prime_triplets` defined here generates an unbounded stream of prime triples, which is harnessed by the generic function `emit_until` defined as follows:
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
The Task:
# Output: [p,p+2,p+6] where p is prime
def prime_triplets:
def pt: .[2] == .[1] + 4 and .[1] == .[0] + 2;
def next: .[1:] + [first( range(.[2] + 2; infinite;2) | select(is_prime))];
# prime the foreach with the first triplet
foreach range(7; infinite; 2) as $i ([2,3,5]; next; select(pt) ) ;
emit_until(.[0] >= 5500; prime_triplets)
- Output:
[5,7,11] [11,13,17] [17,19,23] [41,43,47] [101,103,107] [107,109,113] [191,193,197] [227,229,233] [311,313,317] [347,349,353] [461,463,467] [641,643,647] [821,823,827] [857,859,863] [881,883,887] [1091,1093,1097] [1277,1279,1283] [1301,1303,1307] [1427,1429,1433] [1481,1483,1487] [1487,1489,1493] [1607,1609,1613] [1871,1873,1877] [1997,1999,2003] [2081,2083,2087] [2237,2239,2243] [2267,2269,2273] [2657,2659,2663] [2687,2689,2693] [3251,3253,3257] [3461,3463,3467] [3527,3529,3533] [3671,3673,3677] [3917,3919,3923] [4001,4003,4007] [4127,4129,4133] [4517,4519,4523] [4637,4639,4643] [4787,4789,4793] [4931,4933,4937] [4967,4969,4973] [5231,5233,5237] [5477,5479,5483]
Julia
using Primes
pmask = primesmask(1, 5505)
foreach(n -> println([n, n + 2, n + 6]), filter(n -> pmask[n] && pmask[n + 2] && pmask[n + 6], 1:5500))
- Output:
[5, 7, 11] [11, 13, 17] [17, 19, 23] [41, 43, 47] [101, 103, 107] [107, 109, 113] [191, 193, 197] [227, 229, 233] [311, 313, 317] [347, 349, 353] [461, 463, 467] [641, 643, 647] [821, 823, 827] [857, 859, 863] [881, 883, 887] [1091, 1093, 1097] [1277, 1279, 1283] [1301, 1303, 1307] [1427, 1429, 1433] [1481, 1483, 1487] [1487, 1489, 1493] [1607, 1609, 1613] [1871, 1873, 1877] [1997, 1999, 2003] [2081, 2083, 2087] [2237, 2239, 2243] [2267, 2269, 2273] [2657, 2659, 2663] [2687, 2689, 2693] [3251, 3253, 3257] [3461, 3463, 3467] [3527, 3529, 3533] [3671, 3673, 3677] [3917, 3919, 3923] [4001, 4003, 4007] [4127, 4129, 4133] [4517, 4519, 4523] [4637, 4639, 4643] [4787, 4789, 4793] [4931, 4933, 4937] [4967, 4969, 4973] [5231, 5233, 5237] [5477, 5479, 5483]
Mathematica / Wolfram Language
Cases[Partition[Most@NestWhileList[NextPrime, 2, # < 5500 &], 3,
1], _?(Differences[#] == {2, 4} &)] // TableForm
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
Nim
import strformat
const
N = 5500 - 1
Max = N + 6
# Sieve of Erathosthenes: false (default) is composite.
var composite: array[3..Max, bool] # Ignore 2 as all primes should be odd.
var n = 3
while true:
let n2 = n * n
if n2 > Max: break
if not composite[n]:
for k in countup(n2, Max, 2 * n):
composite[k] = true
inc n, 2
template isPrime(n: int): bool = not composite[n]
echo " p p+2 p+6"
var count = 0
for n in countup(3, N, 2):
if n.isPrime and (n + 2).isPrime and (n + 6).isPrime:
echo &"{n:4} {n+2:4} {n+6:4}"
inc count
echo &"\nFound {count} primes triplets for p < {N+1}."
- Output:
p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes triplets for p < 5500.
PARI/GP
for(i=1,5499,if(isprime(i)&&isprime(i+2)&&isprime(i+6),print(i," ",i+2," ",i+6)))
Perl
#!/usr/bin/perl
use strict;
use warnings;
use ntheory qw( is_prime twin_primes );
is_prime($_ + 6) and printf "%5d" x 3 . "\n", $_, $_ + 2, $_ + 6
for @{ twin_primes( 5500 ) };
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
Phix
function pt(integer p) return is_prime(p+2) and is_prime(p+6) end function sequence res = filter(get_primes_le(5500),pt) res = apply(true,sq_add,{res,{{0,2,6}}}) res = apply(true,sprintf,{{"(%d %d %d)"},res}) printf(1,"Found %d prime triplets: %s\n",{length(res),join(shorten(res,"",2),", ")})
- Output:
Found 43 prime triplets: (5 7 11), (11 13 17), ..., (5231 5233 5237), (5477 5479 5483)
Python
#!/usr/bin/python
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
if __name__ == '__main__':
for p in range(3, 5499, 2):
if not isPrime(p+6):
continue
if not isPrime(p+2):
continue
if not isPrime(p):
continue
print(f'[{p} {p+2} {p+6}]')
Raku
Adapted from Cousin primes
Filter
Favoring brevity over efficiency due to the small range of n, the most concise solution is:
say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;
- Output:
((5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483))
Infinite List
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 :
- All terms are congruent to 5 (mod 6).
constant @triples = (5, *+6 … *).map: -> \n { $_ if .all.is-prime given (n, n+2, n+6) }
my $count = @triples.first: :k, *.[0] >= 5500;
say .fmt('%4d') for @triples.head($count);
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
REXX
/*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
- 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
load "stdlib.ring"
see "working..." + nl
see "Initial members of prime triples (p, p+2, p+6) are:" + nl
see "p p+2 p+6" + nl
row = 0
limit = 5500
for n = 1 to limit
if isprime(n) and isprime(n+2) and isprime(n+6)
row = row + 1
see "" + n + " " + (n+2) + " " + (n+6) + nl
ok
next
see "Found " + row + " primes" + nl
see "done..." + nl
- Output:
working... Initial members of prime triples (p, p+2, p+6) are: p p+2 p+6 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 Found 43 primes done...
Sidef
say "Values of p such that (p, p+2, p+6) are all prime:"
5500.primes.grep{|p| all_prime(p+2, p+6) }.say
- Output:
Values of p such that (p, p+2, p+6) are all prime: [5, 11, 17, 41, 101, 107, 191, 227, 311, 347, 461, 641, 821, 857, 881, 1091, 1277, 1301, 1427, 1481, 1487, 1607, 1871, 1997, 2081, 2237, 2267, 2657, 2687, 3251, 3461, 3527, 3671, 3917, 4001, 4127, 4517, 4637, 4787, 4931, 4967, 5231, 5477]
Tiny BASIC
LET A = 1
10 LET A = A + 2
IF A > 5499 THEN END
LET P = A
GOSUB 100
IF Z = 0 THEN GOTO 10
LET P = A + 2
GOSUB 100
IF Z = 0 THEN GOTO 10
LET P = A + 6
GOSUB 100
IF Z = 0 THEN GOTO 10
PRINT A," ",A+2," ",A+6
GOTO 10
100 REM PRIMALITY BY TRIAL DIVISION
LET Z = 1
LET I = 2
110 IF (P/I)*I = P THEN LET Z = 0
IF Z = 0 THEN RETURN
LET I = I + 1
IF I*I <= P THEN GOTO 110
RETURN
Wren
import "/math" for Int
import "/fmt" for Fmt
var c = Int.primeSieve(5505, false)
var triples = []
System.print("Prime triplets: p, p + 2, p + 6 where p < 5,500:")
var i = 3
while (i < 5500) {
if (!c[i] && !c[i+2] && !c[i+6]) triples.add([i, i+2, i+6])
i = i + 2
}
for (triple in triples) Fmt.print("$,6d", triple)
System.print("\nFound %(triples.count) such prime triplets.")
- Output:
Prime triplets: p, p + 2, p + 6 where p < 5,500: 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1,091 1,093 1,097 1,277 1,279 1,283 1,301 1,303 1,307 1,427 1,429 1,433 1,481 1,483 1,487 1,487 1,489 1,493 1,607 1,609 1,613 1,871 1,873 1,877 1,997 1,999 2,003 2,081 2,083 2,087 2,237 2,239 2,243 2,267 2,269 2,273 2,657 2,659 2,663 2,687 2,689 2,693 3,251 3,253 3,257 3,461 3,463 3,467 3,527 3,529 3,533 3,671 3,673 3,677 3,917 3,919 3,923 4,001 4,003 4,007 4,127 4,129 4,133 4,517 4,519 4,523 4,637 4,639 4,643 4,787 4,789 4,793 4,931 4,933 4,937 4,967 4,969 4,973 5,231 5,233 5,237 5,477 5,479 5,483 Found 43 such prime triplets.
XPL0
func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
int Count, P;
[ChOut(0, ^ );
Count:= 0;
P:= 3;
repeat if IsPrime(P) & IsPrime(P+2) & IsPrime(P+6) then
[IntOut(0, P); ChOut(0, ^ );
IntOut(0, P+2); ChOut(0, ^ );
IntOut(0, P+6); ChOut(0, ^ );
Count:= Count+1;
if rem(Count/5) then ChOut(0, 9\tab\) else CrLf(0);
];
P:= P+2;
until P >= 5500;
CrLf(0);
IntOut(0, Count);
Text(0, " prime triplets found below 5500.
");
]
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483 43 prime triplets found below 5500.