Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions

→‎{{header|REXX}}: updated with an optimized version that skips all of the even integers (except zero).
(→‎{{header|REXX}}: added the computer programming language REXX.)
(→‎{{header|REXX}}: updated with an optimized version that skips all of the even integers (except zero).)
Line 158:
The use of REXX's BIFs to convert decimal numbers to binary and hexadecimal were used   (instead of the   '''base'''  
<br>function) &nbsp; because they are much faster).
 
This REXX version takes advantage that no even integers need be tested &nbsp; (except for the single exception: &nbsp; zero).
<lang rexx>/*REXX pgm finds non─neg integers that are palindromes in base 2, 4, and 16, where N<25k*/
numeric digits 100 /*ensure enough dec. digs for large #'s*/
Line 168 ⟶ 170:
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */
found$= right(0;, w+1) idx= 1 /*numberlist of findsnumbers found (so far);. set IDX. */
$found= 1 /*list# of numbers found finds (so far). , the only even #.*/
idx= 1 do j=0 for n /*findset intthe palindromesIDX in bases(index) 2,4,16to unity. */
h=do d2x(j) =1 by 2 to n-1 /*convertfind dec.int #palindromes toin hexadecimalbases 2,4,16. */
if h\==reverse d2x(hj) then iterate /*Hex number not palindromic? Skip.*/ /*convert ◄■■■■■■■■dec. a# filterto hexadecimal. */
bif h\= x2b( d2x=reverse(jh) ) + 0 then iterate /*Hex number not palindromic? Skip.*/ /*convert dec.◄■■■■■■■■ #a to hex, then tofilter. binary*/
if b\==reverse x2b(b d2x(j) ) + 0 then iterate /*Binary number not palindromic? Skip.*/*convert dec. # to /*hex, ◄■■■■■■■■then a filter.to binary*/
q=if baseb\==reverse(j, 4b) then iterate /*Binary number not palindromic? Skip.*/ /*convert ◄■■■■■■■■ a decimal integer to base 4filter. */
if q\==reverse base(qj, 4) then iterate /*Base 4 number not palindromic? Skip.*/ /* ◄■■■■■■■■convert a filterdecimal integer to base 4. */
found= found + 1 if q\==reverse(q) then iterate /*Base 4 number not palindromic? Skip.*/ /*bump number◄■■■■■■■■ ofa found such numbersfilter. */
$ found= $found + right(1 commas(j), w) /*add the found number ───►/*bump number $of found listsuch numbers. */
if found // cols \=$= 0$ right( commas(j), w) then iterate /*haveadd the found wenumber populated a───► line of$ output? list. */
say center(idx, 7)'│' if substr($, 2);found // cols $\== 0 then iterate /*display whathave we havepopulated soa farline of output? (cols). */
idx= idx + cols say center(idx, 7)'│' substr($, 2); $= /*bumpdisplay thewhat we indexhave so countfar for the(cols). output*/
idx= idx + cols /*bump the index count for the output*/
end /*j*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/