Numbers whose binary and ternary digit sums are prime: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
→‎{{header|Raku}}: Add a Raku example
m →‎{{header|REXX}}: added the computer programming language REXX.
Line 186:
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX program finds and displays integers whose base 2 and base 3 digit sums are prime.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 200 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w= 10 /*width of a number in any column. */
@b2b3= ' numbers < ' commas(hi) " whose binary and ternary digit sums are prime"
if cols>0 then say ' index │'center(@b2b3, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
finds= 0; idx= 1 /*initialize # of finds and the index. */
$= /*a list of numbers found (so far). */
do j=1 to hi-1 /*find #s whose B2 & B3 sums are prime.*/
b2= sumDig( tBase(j, 2) ); if \!.b2 then iterate /*convert to base2, sum digits*/
b3= sumDig( tBase(j, 3) ); if \!.b3 then iterate /* " " base3 " " */
finds= finds + 1 /*bump the number of found integers. */
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 an integer ──► $ list, allow big#*/
if finds//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(finds) @b2b3
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 ?
sumDig: procedure; parse arg x 1 s 2;do j=2 for length(x)-1;s=s+substr(x,j,1);end;return s
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.=0; @= '2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97'
#= words(@); do p=1 for #; _= word(@, p); !._= 1; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
tBase: procedure; parse arg x,toBase; y=; $= 0123456789
do while x>=toBase; y= substr($, x//toBase+1, 1)y; x= x%toBase
end /*while*/
return substr($, x+1, 1)y</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ numbers < 200 whose binary and ternary digit sums are prime
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 5 6 7 10 11 12 13 17 18 19
11 │ 21 25 28 31 33 35 36 37 41 47
21 │ 49 55 59 61 65 67 69 73 79 82
31 │ 84 87 91 93 97 103 107 109 115 117
41 │ 121 127 129 131 133 137 143 145 151 155
51 │ 157 162 167 171 173 179 181 185 191 193
61 │ 199
 
Found 61 numbers < 200 whose binary and ternary digit sums are prime
</pre>
 
=={{header|Ring}}==