Additive primes: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Walterpachl (talk | contribs) (→{{header|REXX}}: ooRexx conformance and simplification) |
||
Line 3,282: | Line 3,282: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<syntaxhighlight lang="rexx">/*REXX program counts/displays the number of additive primes |
<syntaxhighlight lang="rexx">/*REXX program counts/displays the number of additive primes less than N. */ |
||
Parse Arg n cols . /*get optional number of primes To find*/ |
|||
If n=='' | n==',' Then n= 500 /*Not specified? Then assume default.*/ |
|||
If cols=='' | cols==',' Then cols= 10 /* ' ' ' ' ' */ |
|||
call genP n |
call genP n /*generate all primes under N. */ |
||
w= |
w=5 /*width of a number in any column. */ |
||
title= 'additive primes that are < 'commas(n) |
|||
If cols>0 Then Say ' index ¦'center(title,cols*(w+1)+1) |
|||
If cols>0 Then Say '-------+'center('' ,cols*(w+1)+1,'-') |
|||
found=0 |
|||
⚫ | |||
ol='' /*a list of additive primes (so far). */ |
|||
idx=1 |
|||
⚫ | |||
Do j=1 By 1 |
|||
_= sumDigs(p); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ /* ◄■■■■■■■■ a filter. */ |
|||
p=p.j /*obtain the Jth prime. */ |
|||
If p>n Then Leave /* no more needed */ |
|||
_=sumDigs(p) |
|||
⚫ | |||
If !._ Then Do |
|||
⚫ | |||
found=found+1 /*bump the count of additive primes. */ |
|||
c=commas(p) /*maybe add commas To the number. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Say center(idx,7)'¦' substr(ol,2) /*display what we have so far (cols). */ |
|||
⚫ | |||
idx=idx+10 |
|||
End |
|||
End |
|||
End /*j*/ |
|||
If ol\=='' Then |
|||
Say center(idx,7)'¦' substr(ol,2) /*possible display residual output. */ |
|||
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
|||
If cols>0 Then |
|||
say |
|||
Say '--------'center('',cols*(w+1)+1,'-') |
|||
⚫ | |||
Say |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
/*--------------------------------------------------------------------------------*/ |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
genP: parse arg n; @.1= 2; @.2= 3; @.3= 5; @.4= 7; @.5= 11; @.6= 13 |
|||
/*--------------------------------------------------------------------------------*/ |
|||
!.= 0; !.2= 1; !.3= 1; !.5= 1; !.7= 1; !.11= 1; !.13= 1 |
|||
genP: |
|||
#= 6; sq.#= @.# ** 2 /*the number of primes; prime squared.*/ |
|||
Parse Arg n |
|||
do j=@.#+2 by 2 for max(0, n%2-@.#%2-1) /*find odd primes from here on. */ |
|||
pl=2 3 5 7 11 13 |
|||
⚫ | |||
!.=0 |
|||
if _==5 then iterate; if j// 3==0 then iterate /*J ÷ by 5? J ÷ by 3? */ |
|||
Do np=1 By 1 While pl<>'' |
|||
if j// 7==0 then iterate; if j//11==0 then iterate /*" " " 7? " " " 11? */ |
|||
Parse Var pl p pl |
|||
/* [↓] divide by the primes. ___ */ |
|||
p.np=p |
|||
⚫ | |||
sq.np=p*p |
|||
⚫ | |||
!.p=1 |
|||
end /*k*/ /* [↑] only divide up to √ J */ |
|||
End |
|||
#= # + 1; @.#= j; sq.#= j*j; !.j= 1 /*bump prime count; assign prime & flag*/ |
|||
np=np-1 |
|||
end /*j*/; return</syntaxhighlight> |
|||
Do j=p.np+2 by 2 While j<n |
|||
⚫ | |||
If _==5 Then Iterate |
|||
If j// 3==0 Then Iterate |
|||
If j// 7==0 Then Iterate |
|||
If j//11==0 Then Iterate |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
p.np=j |
|||
sq.np=j*j |
|||
!.j=1 |
|||
End /*j*/ |
|||
Return</syntaxhighlight> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
index |
index ¦ additive primes that are < 500 |
||
-------+------------------------------------------------------------- |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 |
1 ¦ 2 3 5 7 11 23 29 41 43 47 |
||
11 |
11 ¦ 61 67 83 89 101 113 131 137 139 151 |
||
21 |
21 ¦ 157 173 179 191 193 197 199 223 227 229 |
||
31 |
31 ¦ 241 263 269 281 283 311 313 317 331 337 |
||
41 |
41 ¦ 353 359 373 379 397 401 409 421 443 449 |
||
51 |
51 ¦ 461 463 467 487 |
||
--------------------------------------------------------------------- |
|||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
found 54 |
found 54 additive primes that are < 500 |
||
</pre> |
</pre> |
||