Additive primes: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed the GENP subroutine.)
(→‎{{header|REXX}}: added/changed whitespace and comments, optimized the program.)
Line 870: Line 870:
call genP n /*generate all primes under N. */
call genP n /*generate all primes under N. */
w= 10 /*width of a number in any column. */
w= 10 /*width of a number in any column. */
title= " additive primes that are < " n
title= " additive primes that are < " commas(n)
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Aprimes= 0; idx= 1 /*initialize # of additive primes & idx*/
found= 0; idx= 1 /*initialize # of additive primes & IDX*/
$= /*a list of additive primes (so far). */
$= /*a list of additive primes (so far). */
do j=2 until j>=n; if \!.j then iterate /*Is J not a prime? No, then skip it.*/ /* ◄■■■■■■■■ a filter. */
do j=1 for #; p= @.j /*obtain the Jth prime. */
_= sumDigs(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ /* ◄■■■■■■■■ a filter. */
_= sumDigs(p); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ /* ◄■■■■■■■■ a filter. */
Aprimes= Aprimes + 1 /*bump the count of additive primes. */
found= found + 1 /*bump the count of additive primes. */
if cols<0 then iterate /*Build the list (to be shown later)? */
if cols<0 then iterate /*Build the list (to be shown later)? */
c= commas(j) /*maybe add commas to the number. */
c= commas(p) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add additive prime──►list, allow big#*/
$= $ right(c, max(w, length(c) ) ) /*add additive prime──►list, allow big#*/
if Aprimes//cols\==0 then iterate /*have we populated a line of output? */
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
idx= idx + cols /*bump the index count for the output*/
Line 889: Line 889:
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say
say 'found ' commas(Aprimes) title
say 'found ' commas(found) title
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 895: Line 895:
sumDigs: parse arg x 1 s 2; do k=2 for length(x)-1; s= s + substr(x,k,1); end; return s
sumDigs: parse arg x 1 s 2; do k=2 for length(x)-1; s= s + substr(x,k,1); end; return s
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: parse arg n; @.=.; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; #= 7
genP: parse arg n; @.1= 2; @.2= 3; @.3= 5; @.4= 7; @.5= 11; @.6= 13
w= length(n); !.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1
!.= 0; !.2= 1; !.3= 1; !.5= 1; !.7= 1; !.11= 1; !.13= 1
do j=@.7+2 by 2 while j<n /*continue on with the next odd prime. */
#= 6; sq.#= @.# ** 2 /*the number of primes; prime squared.*/
parse var j '' -1 _ /*obtain the last digit of the J var.*/
do j=@.#+2 by 2 for max(0, n%2-@.#%2-1) /*find odd primes from here on. */
if _==5 then iterate; if j//3==0 then iterate /*J ÷ by 5? J ÷ by 3? */
parse var j '' -1 _ /*obtain the last digit of the J var.*/
if _==5 then iterate; if j// 3==0 then iterate /*J ÷ by 5? J ÷ by 3? */
if j// 7==0 then iterate; if j//11==0 then iterate /*" " " 7? " " " 11? */
/* [↓] divide by the primes. ___ */
/* [↓] divide by the primes. ___ */
do k=4 to # while k*k<=j /*divide J by other primes ≤ √ J */
do k=6 while sq.k<=j /*divide J by other primes ≤ √ J */
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */
if j//@.k==0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
end /*k*/ /* [↑] only divide up to √ J */
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
#= # + 1; @.#= j; sq.#= j*j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/; return</lang>
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>