Primes whose sum of digits is 25: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(→‎{{header|Wren}}: Correction to libheader.)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 274:
997 1699 1789 1879 1987 2689 2797 2887 3499 3697
3769 3877 3967 4597 4759 4957 4993</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds and displays primes less than HI whose decimal digits sum to TARGET.*/
parse arg hi cols target . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 5000 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
if target=='' | target=="," then target= 25 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w= 10 /*width of a number in any column. */
@primes= ' primes that are < ' commas(hi) " and whose decimal digits sum to "
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 /*initialize # of primes found & index.*/
$= /*a list of nice primes (so far). */
do j=2 for hi-2; if \!.j then iterate /*Is J a prime? No, then skip it. */
if sumDigs(j) \== 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(j) /*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*/
end /*j*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say
say 'Found ' commas(primesT) @primes commas(target)
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: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 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5; 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? */
/* [↑] the above five lines saves time*/
do k=5 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>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ primes that are < 5,000 and whose decimal digits sum to 25
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 997 1,699 1,789 1,879 1,987 2,689 2,797 2,887 3,499 3,697
11 │ 3,769 3,877 3,967 4,597 4,759 4,957 4,993
 
Found 17 primes that are < 5,000 and whose decimal digits sum to 25
</pre>
 
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1000000 &nbsp; 0 </tt>}}
<pre>
Found 6,198 primes that are < 1,000,000 and whose decimal digits sum to 25
</pre>
 
=={{header|Ring}}==