Additive primes: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
m (→{{header|REXX}}: added/changed comments and whitespace, highlighted the filters, added a foot separator, simplified the code.) |
||
Line 846: | Line 846: | ||
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 |
|||
if cols>0 then say ' |
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) |
||
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') |
|||
Aprimes= 0; idx= 1 /*initialize # of additive primes & idx*/ |
Aprimes= 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.*/ |
do j=2 until j>=n; if \!.j then iterate /*Is J not a prime? No, then skip it.*/ /* ◄■■■■■■■■ a filter. */ |
||
_= sumDigs(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ |
_= sumDigs(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ /* ◄■■■■■■■■ a filter. */ |
||
Aprimes= Aprimes + 1 /*bump the count of additive primes. */ |
Aprimes= Aprimes + 1 /*bump the count of additive primes. */ |
||
if cols |
if cols<0 then iterate /*Build the list (to be shown later)? */ |
||
c= commas(j) /*maybe add commas to the number. */ |
c= commas(j) /*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#*/ |
||
Line 862: | Line 863: | ||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
||
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
|||
say |
say |
||
say 'found ' commas(Aprimes) |
say 'found ' commas(Aprimes) title |
||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
commas: parse arg ?; |
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
||
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 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 880: | Line 882: | ||
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; !.j= 1 /*bump prime count; assign prime & flag*/ |
||
end /*j*/ |
end /*j*/; return</lang> |
||
return</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 892: | Line 893: | ||
41 │ 353 359 373 379 397 401 409 421 443 449 |
41 │ 353 359 373 379 397 401 409 421 443 449 |
||
51 │ 461 463 467 487 |
51 │ 461 463 467 487 |
||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
found 54 additive primes < 500 |
found 54 additive primes that are < 500 |
||
</pre> |
</pre> |
||