Anonymous user
Additive primes: Difference between revisions
m
→{{header|REXX}}: changed the format of the output.
(Added C++ and Forth solutions) |
m (→{{header|REXX}}: changed the format of the output.) |
||
Line 282:
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " */
call genP n /*generate all primes under N. */
if cols>0 then say ' index │'center(" additive primes that are < " n, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
Aprimes= 0;
$= /*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.*/
_= sumDigs(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/
if
c= commas(j)
▲ $= $ right(j, w) /*add the additive prime to the $ list.*/
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)
say
say 'found '
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 ?
sumDigs: parse arg x 1 s 2; do k=2 for length(x)-1; s= s + substr(x,k,1); end; return s
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 317 ⟶ 322:
{{out|output|text= when using the default inputs:}}
<pre>
index │
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 2 3 5 7 11 23 29 41 43 47
11 │ 61 67 83 89 101 113 131 137 139 151
21 │ 157 173 179 191 193 197 199 223 227 229
31 │ 241 263 269 281 283 311 313 317 331 337
41 │ 353 359 373 379 397 401 409 421 443 449
51 │ 461 463 467 487
found 54 additive primes < 500
|