Prime triplets: Difference between revisions

m
syntax highlighting fixup automation
(Added Sidef)
m (syntax highlighting fixup automation)
Line 16:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(n)
I n == 2
R 1B
Line 33:
count++
 
print("\nFound "count‘ primes triplets for p < 5500.’)</langsyntaxhighlight>
 
{{out}}
Line 87:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
Line 104:
OD
PrintF("%E%EThere are %I prime triplets",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Prime_triplets.png Screenshot from Atari 8-bit computer]
Line 120:
=={{header|ALGOL 68}}==
Using code from [[Successive_prime_differences#ALGOL_68]]
<langsyntaxhighlight lang="algol68">BEGIN # find primes p where p+2 and p+6 are also prime #
# reurns a list of primes up to n #
PROC prime list = ( INT n )[]INT:
Line 175:
print( ( "Prime triplets under ", whole( max number, 0 ), ":", newline ) );
try differences( p list, ( 2, 4 ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 192:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">lst: select select 2..5500 => prime? 'x
-> and? [prime? x+2] [prime? x+6]
 
Line 199:
pad join.with:", "
to [:string] @[item item+2 item+6] 17
]</langsyntaxhighlight>
 
{{out}}
Line 214:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIME_TRIPLETS.AWK
BEGIN {
Line 239:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 291:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 308:
print "["; p; " "; p+2; " "; p+6; "]"
next p
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 344:
Next p
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">
sub isPrime(v)
if v < 2 then return False : fi
Line 365:
print "[", p using "####", p+2 using "####", p+6 using "####", "]"
next p
end</langsyntaxhighlight>
 
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<langsyntaxhighlight lang="factor">USING: arrays kernel lists lists.lazy math math.primes
math.primes.lists prettyprint sequences ;
 
Line 377:
[ [ prime? ] all? ] lfilter ! Select triplets which contain only primes
[ first 5500 < ] lwhile ! Make the list end eventually...
[ . ] leach ! Print each item in the list</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 426:
 
=={{header|Fermat}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
for p as uinteger = 3 to 5499 step 2
if not isprime(p+6) then continue for
Line 435:
if not isprime(p) then continue for
print using "[#### #### ####] ";p;p+2;p+6;
next p</langsyntaxhighlight>
{{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]
Line 443:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 467:
}
fmt.Println("\nFound", len(triples), "such prime triplets.")
}</langsyntaxhighlight>
 
{{out}}
Line 475:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 FOR A = 3 TO 5499 STEP 2
20 P = A
30 GOSUB 1000
Line 491:
1020 I = I + 1
1030 IF I*I > P THEN RETURN
1040 GOTO 1010</langsyntaxhighlight>
 
=={{header|jq}}==
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:
<syntaxhighlight lang="jq">
<lang jq>
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</langsyntaxhighlight>
The Task:
<langsyntaxhighlight lang="jq"># Output: [p,p+2,p+6] where p is prime
def prime_triplets:
def pt: .[2] == .[1] + 4 and .[1] == .[0] + 2;
Line 510:
foreach range(7; infinite; 2) as $i ([2,3,5]; next; select(pt) ) ;
 
emit_until(.[0] >= 5500; prime_triplets) </langsyntaxhighlight>
{{out}}
<pre>
Line 559:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
pmask = primesmask(1, 5505)
foreach(n -> println([n, n + 2, n + 6]), filter(n -> pmask[n] && pmask[n + 2] && pmask[n + 6], 1:5500))
</langsyntaxhighlight>{{out}}
<pre>
[5, 7, 11]
Line 610:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Cases[Partition[Most@NestWhileList[NextPrime, 2, # < 5500 &], 3,
1], _?(Differences[#] == {2, 4} &)] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 660:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
const
Line 686:
inc count
 
echo &"\nFound {count} primes triplets for p < {N+1}."</langsyntaxhighlight>
 
{{out}}
Line 737:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">for(i=1,5499,if(isprime(i)&&isprime(i+2)&&isprime(i+6),print(i," ",i+2," ",i+6)))</langsyntaxhighlight>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 748:
 
is_prime($_ + 6) and printf "%5d" x 3 . "\n", $_, $_ + 2, $_ + 6
for @{ twin_primes( 5500 ) };</langsyntaxhighlight>
{{out}}
<pre>
Line 797:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">pt</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5500</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">)</span>
Line 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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d prime triplets: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 811:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 827:
if not isPrime(p):
continue
print(f'[{p} {p+2} {p+6}]')</langsyntaxhighlight>
 
 
Line 834:
===Filter===
Favoring brevity over efficiency due to the small range of n, the most concise solution is:
<syntaxhighlight lang="raku" perl6line>say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;</langsyntaxhighlight>
{{out}}
<pre>
Line 842:
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 :
:All terms are congruent to 5 (mod 6).
<syntaxhighlight lang="raku" perl6line>constant @triples = (5, *+6 … *).map: -> \n { $_ if .all.is-prime given (n, n+2, n+6) }
 
my $count = @triples.first: :k, *.[0] >= 5500;
 
say .fmt('%4d') for @triples.head($count);</langsyntaxhighlight>
{{out}}
<pre>
Line 894:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds prime triplets: P, P+2, P+6 are primes, and P < some specified N*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 5500 /*Not specified? Then use the default.*/
Line 941:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 963:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 980:
see "Found " + row + " primes" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,035:
 
=={{header|Sidef}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
{{out}}
<pre>
Line 1,044:
 
=={{header|Tiny BASIC}}==
<langsyntaxhighlight lang="tinybasic"> LET A = 1
10 LET A = A + 2
IF A > 5499 THEN END
Line 1,065:
LET I = I + 1
IF I*I <= P THEN GOTO 110
RETURN</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 1,082:
}
for (triple in triples) Fmt.print("$,6d", triple)
System.print("\nFound %(triples.count) such prime triplets.")</langsyntaxhighlight>
 
{{out}}
Line 1,135:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,160:
Text(0, " prime triplets found below 5500.
");
]</langsyntaxhighlight>
 
{{out}}
10,327

edits