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) because they are much faster). |
<br>function) because they are much faster). |
||
This REXX version takes advantage that no even integers need be tested (except for the single exception: 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 " " " */ |
||
$= right(0, w+1) /*list of numbers found (so far). */ |
|||
found= 1 /*# of finds (so far), the only even #.*/ |
|||
idx= 1 /*set the IDX (index) to unity. */ |
|||
do j=1 by 2 to n-1 /*find int palindromes in bases 2,4,16.*/ |
|||
h= d2x(j) /*convert dec. # to hexadecimal. */ |
|||
if h\==reverse(h) then iterate /*Hex number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */ |
|||
b= x2b( d2x(j) ) + 0 /*convert dec. # to hex, then to binary*/ |
|||
if b\==reverse(b) then iterate /*Binary number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */ |
|||
q= base(j, 4) /*convert a decimal integer to base 4. */ |
|||
if q\==reverse(q) then iterate /*Base 4 number not palindromic? Skip.*/ /* ◄■■■■■■■■ a filter. */ |
|||
found= found + 1 /*bump number of found such numbers. */ |
|||
$= $ right( commas(j), w) /*add the found number ───► $ list. */ |
|||
if found // 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*/ |
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.*/ |