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

Content added Content deleted
(→‎{{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: Line 158:
The use of REXX's BIFs to convert decimal numbers to binary and hexadecimal were used   (instead of the   '''base'''  
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).
<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*/
<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*/
numeric digits 100 /*ensure enough dec. digs for large #'s*/
Line 168: Line 170:
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */
found= 0; idx= 1 /*number of finds (so far); set IDX. */
$= right(0, w+1) /*list of numbers found (so far). */
$= /*list of numbers found (so far). */
found= 1 /*# of finds (so far), the only even #.*/
do j=0 for n /*find int palindromes in bases 2,4,16.*/
idx= 1 /*set the IDX (index) to unity. */
h= d2x(j) /*convert dec. # to hexadecimal. */
do j=1 by 2 to n-1 /*find int palindromes in bases 2,4,16.*/
if h\==reverse(h) then iterate /*Hex number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */
h= d2x(j) /*convert dec. # to hexadecimal. */
b= x2b( d2x(j) ) + 0 /*convert dec. # to hex, then to binary*/
if h\==reverse(h) then iterate /*Hex number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */
if b\==reverse(b) then iterate /*Binary number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */
b= x2b( d2x(j) ) + 0 /*convert dec. # to hex, then to binary*/
q= base(j, 4) /*convert a decimal integer to base 4. */
if b\==reverse(b) then iterate /*Binary number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */
if q\==reverse(q) then iterate /*Base 4 number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */
q= base(j, 4) /*convert a decimal integer to base 4. */
found= found + 1 /*bump number of found such numbers. */
if q\==reverse(q) then iterate /*Base 4 number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */
$= $ right( commas(j), w) /*add the found number ───► $ list. */
found= found + 1 /*bump number of found such numbers. */
if found // cols \== 0 then iterate /*have we populated a line of output? */
$= $ right( commas(j), w) /*add the found number ───► $ list. */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
if found // cols \== 0 then iterate /*have we populated a line of output? */
idx= idx + cols /*bump the index count for the 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*/
end /*j*/


if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/