Neighbour primes: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 223:
439 443 194479
487 491 239119
</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX program finds neighbor primes: P, Q, P*Q+2 are primes, and P < some specified N.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 500 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP hi+50 /*build semaphore array for low primes.*/
do p=1 while @.p<hi
end /*p*/; lim= p-1; q= p+1 /*set LIM to prime for P; calc. 2nd HI.*/
call genP @.p * @.q + 2 /*build semaphore array for high primes*/
w= 10 /*width of a number in any column. */
@neig= ' neighbor primes: p, q, p*q+2 are primes, and p < ' commas(hi)
if cols>0 then say ' index │'center(@neig, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Nprimes= 0; idx= 1 /*initialize # neighbor primes & index.*/
$= /*a list of neighbor primes (so far).*/
do j=1 to lim; jp= j+1; q= @.jp /*look for neighbor primes within range*/
x= @.j * q + 2; if \!.x then iterate /*is X also a prime? No, then skip it.*/
Nprimes= Nprimes + 1 /*bump the number of neighbor primes. */
if cols==0 then iterate /*Build the list (to be shown later)? */
$= $ right( commas(@.j), w) /*add neighbor prime ──► the $ list. */
if Nprimes//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 /*j*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(Nprimes) @neig
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 │ neighbor primes: p, q, p*q+2 are primes, and p < 500
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 3 5 7 13 19 67 149 179 229 239
11 │ 241 269 277 307 313 397 401 419 439 487
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
Found 20 neighbor primes: p, q, p*q+2 are primes, and p < 500
</pre>