Prime triplets: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 114:
5231 5233 5237
5477 5479 5483</pre>
 
=={{header|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; p2= @.j+2; p6= p2 + 4 /*look for prime triplets within range.*/
if \!.p2 | \!.p6 then iterate /*Are P2 & P6 prime? No, then skip it.*/
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>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
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
</pre>
 
=={{header|Ring}}==