Additive primes: Difference between revisions
Content deleted Content added
Added XPL0 example. |
m →{{header|REXX}}: added/changed comments and whitespace, highlighted the filters, added a foot separator, simplified the code. |
||
Line 846:
call genP n /*generate all primes under N. */
w= 10 /*width of a number in any column. */
if cols>0 then say '
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Aprimes= 0; idx= 1 /*initialize # of additive primes & idx*/
$= /*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. */
_= 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. */
if cols
c= commas(j) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add additive prime──►list, allow big#*/
Line 862 ⟶ 863:
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(Aprimes)
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?;
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 ⟶ 882:
end /*k*/ /* [↑] only divide up to √ J */
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/; return</lang>
{{out|output|text= when using the default inputs:}}
<pre>
Line 892 ⟶ 893:
41 │ 353 359 373 379 397 401 409 421 443 449
51 │ 461 463 467 487
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
found 54 additive primes that are < 500
</pre>
|