Additive primes: Difference between revisions
Content added Content deleted
(Added C++ and Forth solutions) |
m (→{{header|REXX}}: changed the format of the output.) |
||
Line 282: | Line 282: | ||
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/ |
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/ |
||
if cols=='' | cols=="," then cols= 10 /* " " " " " */ |
if cols=='' | cols=="," then cols= 10 /* " " " " " */ |
||
Ocols= cols; cols= abs(cols) /*Use the absolute value of cols. */ |
|||
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. */ |
|||
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), '─') |
|||
⚫ | |||
$= /*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.*/ |
||
_= 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.*/ |
||
Aprimes= Aprimes + 1 /*bump the count of additive primes. */ |
|||
if |
if cols==0 then iterate /*Build the list (to be shown later)? */ |
||
c= commas(j) |
|||
⚫ | |||
$= $ 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? */ |
|||
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*/ |
end /*j*/ |
||
if $\=='' then say substr($, 2) |
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
||
say |
say |
||
say 'found ' |
say 'found ' commas(Aprimes) " additive primes < " commas(n) |
||
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 ?; 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 317: | Line 322: | ||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
index │ additive primes that are < 500 |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
61 67 83 89 101 113 131 137 139 151 |
|||
1 │ 2 3 5 7 11 23 29 41 43 47 |
|||
157 173 179 191 193 197 199 223 227 229 |
|||
11 │ 61 67 83 89 101 113 131 137 139 151 |
|||
241 263 269 281 283 311 313 317 331 337 |
|||
21 │ 157 173 179 191 193 197 199 223 227 229 |
|||
353 359 373 379 397 401 409 421 443 449 |
|||
31 │ 241 263 269 281 283 311 313 317 331 337 |
|||
461 463 467 487 |
|||
41 │ 353 359 373 379 397 401 409 421 443 449 |
|||
51 │ 461 463 467 487 |
|||
found 54 additive primes < 500 |
found 54 additive primes < 500 |