Consecutive primes with ascending or descending differences: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, optimized the GENP subroutine.
(Promoted to a Task)
m (→‎{{header|REXX}}: added/changed whitespace and comments, optimized the GENP subroutine.)
Line 738:
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !@.1=2; 0 @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; @.8=19 /*placeholdersdefine forlow primes (semaphores).*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. #=8; sq.#= @.# ** 2 /*number of primes so far; prime sqiare*/
!.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 hi; parse var j '' -1 _ /*find odd primes from here on. */
parseif var j '' -1 _;==5 if then iterate; if _j// 3==50 then iterate /*J divisible÷ by 5? (right dig) J ÷ by 3? */
if j// 7==0 then iterate; if j// 311==0 then iterate /*" " 7? " " " " 311? */
if j//13==0 then iterate; if j// 717==0 then iterate /*" " 13? " " 7? " 17? */
do k=8 while sq.k<=j /* [] thedivide above 3by the linesknown savesodd timeprimes.*/
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 sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# square*/
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg ?; if ? then AorD= 'ascending' /*choose which literal for display.*/
else AorD= 'descending' /* " " " " " */
@lrcptitle= ' longest run of consecutive primes whose differences between primes are' ,
'primes are 'strictly' AorD "and < " commas(hi)
say; say; say
if cols>0 then say ' index │'center(@lrcptitle, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Cprimesfound= 0; idx= 1 /*initialize # of consecutive primes. */
$= /*a list of consecutive primes (so far)*/
do o=1 for words(seq.mxrun) /*show all consecutive primes in seq. */
c= commas( word(seq.mxrun, o) ) /*obtain the next prime in the sequence*/
Cprimesfound= Cprimesfound + 1 /*bump the number of consecutive primes*/
if cols=<=0 then iterate /*Buildbuild the list (to be shown later)? */
$= $ right(c, max(w, length(c) ) ) /*add a nice prime ──► list, allow big#*/
if Cprimesfound//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); /*display what we have so far (cols). */
idx= idx + cols; $= $= /*bump the index count for the output*/
end /*o*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*maybe show residual output*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say; say commas(Cprimes) ' was the'@lrcptitle; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 779 ⟶ 777:
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 128,981 128,983 128,987 128,993 129,001 129,011 129,023 129,037
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
8 was the longest run of consecutive primes whose differences between primes are strictly ascending and < 1,000,000
Line 787 ⟶ 786:
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 322,171 322,193 322,213 322,229 322,237 322,243 322,247 322,249
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
8 was the longest run of consecutive primes whose differences between primes are strictly descending and < 1,000,000