Additive primes: Difference between revisions

Content added Content deleted
(→‎{{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 under a specified number N.*/
<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*/
Parse Arg n cols . /*get optional number of primes To find*/
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 /* ' ' ' ' ' */
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. */
w=5 /*width of a number in any column. */
title= " additive primes that are < " commas(n)
title= 'additive primes that are < 'commas(n)
if cols>0 then say ' index 'center(title, 1 + cols*(w+1) )
If cols>0 Then Say ' index ¦'center(title,cols*(w+1)+1)
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '')
If cols>0 Then Say '-------+'center('' ,cols*(w+1)+1,'-')
found=0
found= 0; idx= 1 /*initialize # of additive primes & IDX*/
$= /*a list of additive primes (so far). */
ol='' /*a list of additive primes (so far). */
idx=1
do j=1 for #; p= @.j /*obtain the Jth prime. */
Do j=1 By 1
_= sumDigs(p); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ /* ◄■■■■■■■■ a filter. */
found= found + 1 /*bump the count of additive primes. */
p=p.j /*obtain the Jth prime. */
if cols<0 then iterate /*Build the list (to be shown later)? */
If p>n Then Leave /* no more needed */
_=sumDigs(p)
c= commas(p) /*maybe add commas to the number. */
If !._ Then Do
$= $ right(c, max(w, length(c) ) ) /*add additive prime──►list, allow big#*/
if found//cols\==0 then iterate /*have we populated a line of output? */
found=found+1 /*bump the count of additive primes. */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
c=commas(p) /*maybe add commas To the number. */
ol=ol right(c,max(w,length(c))) /*add additive prime--?list,allow big# */
idx= idx + cols /*bump the index count for the output*/
If words(ol)=10 Then Do /* a line is complete */
end /*j*/
Say center(idx,7)'¦' substr(ol,2) /*display what we have so far (cols). */
ol='' /* prepare for next line */
idx=idx+10
End
End
End /*j*/


If ol\=='' Then
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
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 'found ' commas(found) title
Say
exit 0 /*stick a fork in it, we're all done. */
Say 'found ' commas(found) title
/*──────────────────────────────────────────────────────────────────────────────────────*/
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
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
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
parse var j '' -1 _ /*obtain the last digit of the J var.*/
!.=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
do k=6 while sq.k<=j /*divide J by other primes ≤ √ J */
sq.np=p*p
if j//@.k==0 then iterate j /*÷ by prev. prime? ¬prime ___ */
!.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
Parse Var j '' -1 _ /*obtain the last digit of the J var.*/
If _==5 Then Iterate
If j// 3==0 Then Iterate
If j// 7==0 Then Iterate
If j//11==0 Then Iterate
Do k=6 By 1 While sq.k<=j /*divide J by other primes <=sqrt(j) */
If j//p.k==0 Then Iterate j /* not prime - try next */
End /*k*/
np=np+1 /*bump prime count; assign prime & flag*/
p.np=j
sq.np=j*j
!.j=1
End /*j*/
Return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
index additive primes that are < 500
index ¦ additive primes that are < 500
-------+-------------------------------------------------------------
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 2 3 5 7 11 23 29 41 43 47
1 ¦ 2 3 5 7 11 23 29 41 43 47
11 61 67 83 89 101 113 131 137 139 151
11 ¦ 61 67 83 89 101 113 131 137 139 151
21 157 173 179 191 193 197 199 223 227 229
21 ¦ 157 173 179 191 193 197 199 223 227 229
31 241 263 269 281 283 311 313 317 331 337
31 ¦ 241 263 269 281 283 311 313 317 331 337
41 353 359 373 379 397 401 409 421 443 449
41 ¦ 353 359 373 379 397 401 409 421 443 449
51 461 463 467 487
51 ¦ 461 463 467 487
---------------------------------------------------------------------
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────


found 54 additive primes that are < 500
found 54 additive primes that are < 500
</pre>
</pre>