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 /* " " " " " */
Ocols= cols; cols= abs(cols) /*Use the absolute value of cols. */
call genP n /*generate all primes under N. */
primesw= 010 /*initializewidth of a number ofin additiveany primescolumn. */
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; $= $ right(j, w) idx= 1 /*addinitialize the# of additive prime to theprimes $& list.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.*/
_= sumDigs(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/
primesAprimes= primesAprimes + 1 /*bump the count of additive primes. */
if Ocols<1cols==0 then iterate /*Build the list (to be shown later)? */
c= commas(j)
$= $ right(j, w) /*add the additive prime to the $ list.*/
if primes//cols\=$=0 $ thenright(c, iteratemax(w, length(c) ) ) /*have we populated a lineadd ofadditive output?prime──►list, allow big#*/
sayif substr($, 2); $Aprimes//cols\==0 then iterate /*display whathave we havepopulated soa farline of output? (cols). */
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) /*possible display some residual output.*/
say
say 'found ' primescommas(Aprimes) " additive primes < " commas(n)
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=&nbsp; when using the default inputs:}}
<pre>
index 2 3 5 7 11 23 29 41 43 47 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