Anonymous user
Triplet of three numbers: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed algorithm.
(Added Sidef) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed algorithm.) |
||
Line 1,313:
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds prime triplets:
parse arg hi cols .
if hi=='' | hi=="," then hi= 6000
if cols>0 then say '───────┼'center("" ,
found= 0; idx=
$=
do j=1 for
p3= j + 3;
p5= j + 5; if \!.p5 then iterate /* " P5 " "
found= found + 1 /*bump the number of
if cols<0 then iterate /*Build the list (to be shown later)? */
end /*j*/▼
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*/
if $\=='' then say center(idx, 7)"│" strip(substr($, 2), 'T') /*possible show residual*/
if cols>0 then say '───────┴'center("" , 1 + cols*(
say
say 'Found '
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas:
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP:
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* "
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 to hip /*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? */
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= when using the default inputs:}}
<pre>
index │
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Found 46 prime triplets:
</pre>
|