Nice primes: Difference between revisions

188 bytes removed ,  2 years ago
m
→‎{{header|REXX}}: simplified the code.
m (→‎{{header|REXX}}: simplified the code.)
Line 1,095:
call genP /*build array of semaphores for primes.*/
w= 10 /*width of a number in any column. */
@nicetitle= ' nice primes that are between ' commas(lo) " and " commas(hi)
if cols>0 then say ' index │'center(@nicetitle ' (not inclusive)', 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Nprimesfound= 0; idx= 1 /*initialize # of nice primes and index*/
$= /*a list of nice primes (so far). */
do j=lo+1 to hi-1; if \!.j then iterate /*search for nice primes within range*/
digRoot= 1 + (j - 1) // 9 /*obtain the digital root of J. */
if \!.digRoot then iterate /*Is digRoot prime? No, then skip it.*/
Nprimesfound= Nprimesfound + 1 /*bump the number of nice primes. */
if cols==<0 then iterate /*Build the list (to be shown later)? */
c= commas(j) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add a nice prime ──► list, allow big#*/
if Nprimesfound//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*/
Line 1,115:
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(Nprimesfound) @nicetitle ' (not inclusive).'
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 @.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. /*placeholders for primes (semaphores).*/
@!.1=20; @!.2=31; @!.3=51; @!.45=1; !.7=1; @!.5=11=1 /* " /*define some low primes. " " " semaphores. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " "#=5; "s.#= @.# **2 " /*number of primes so flags. far; prime². */
#=5; s.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 to hi /*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 five 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. ___ */