Primes whose sum of digits is 25: Difference between revisions

m
→‎{{header|REXX}}: used faster versions of SUMDIGS and GENP.
(→‎{{header|REXX}}: added the computer programming language REXX.)
m (→‎{{header|REXX}}: used faster versions of SUMDIGS and GENP.)
Line 286:
if cols>0 then say ' index │'center(@primes commas(target), 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
primesT= 0; idx= 1 /*initializedefine # oftarget primes found & index.*/
$= /*a list of nice target primes found (so far). */
do j=21 for hi-2#; if \! y= @.j then iterate /*Is J a prime? No, then skip it /*examine all the primes generated. */
if sumDigs(jy) \== target then iterate /*Is sum≡target sum? No, then skip it.*/
primesT= primesT + 1 /*bump the number of target primes. */
if cols == 0 then iterate /*Build the list (to be shown later)? */
c= commas(jy) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add a prime ──► list, allow big #'s.*/
if primesT//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*/
Line 305:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
sumDigs:procedure; arg x 1 s 2; do j=2 for length(x)-1; s= s+substr(x,j,1); end; return s
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0 /*placeholders for primes' (semaphores). */
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1; @.13=1 /* " " " primes' " flagssemaphores. */
#=5 6; s.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 to hi /*find odd primes from here on. */
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/
if j// 3==0 then iterate /*" " " 3? */
if j// 7==0 then iterate /*" " " 7? */
if j//11==0 then iterate /*" " " 11? /* [↑] the above five lines saves time*/
do k=56 while s.k<=j /* [↓] divide by the known odd primes.*/
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDigs: parse arg x 1 s 2 '' -1 z; L= length(x); if L==1 then return s; s= s + z
sumDigs:procedure; arg x 1 s 2; do jm=2 for length(x)L-12; s= s + substr(x,j m, 1); end; return s</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>