Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Content added Content deleted
(Add Factor) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 151: | Line 151: | ||
273 771 819 1285 1365 3855 4095 4097 4369 12291 |
273 771 819 1285 1365 3855 4095 4097 4369 12291 |
||
13107 20485 21845</pre> |
13107 20485 21845</pre> |
||
=={{header|REXX}}== |
|||
Programming note: the conversions of a decimal number to another base (radix) was ordered such that the fastest |
|||
<br>base conversion was performed before the other conversions. |
|||
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). |
|||
<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*/ |
|||
parse arg n cols . /*obtain optional argument from the CL.*/ |
|||
if n=='' | n=="," then n = 25000 /*Not specified? Then use the default.*/ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
|||
w= 10 /*width of a number in any column. */ |
|||
title= ' non-negative integers that are palindromes in base 2, 4, and 16, where N < ' , |
|||
commas(n) |
|||
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */ |
|||
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */ |
|||
found= 0; idx= 1 /*number of finds (so far); set IDX. */ |
|||
$= /*list of numbers found (so far). */ |
|||
do j=0 for n /*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*/ |
|||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
|||
say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */ |
|||
say |
|||
say 'Found ' found title |
|||
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 ? |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
base: procedure; parse arg #,t,,y; @= 0123456789abcdefghijklmnopqrstuvwxyz /*up to 36*/ |
|||
@@= substr(@, 2); do while #>=t; y= substr(@, #//t + 1, 1)y; #= # % t |
|||
end; return substr(@, #+1, 1)y</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
index │ non-negative integers that are palindromes in base 2, 4, and 16, where N < 25,000 |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 0 1 3 5 15 17 51 85 255 257 |
|||
11 │ 273 771 819 1,285 1,365 3,855 4,095 4,097 4,369 12,291 |
|||
21 │ 13,107 20,485 21,845 |
|||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
Found 23 non-negative integers that are palindromes in base 2, 4, and 16, where N < 25,000 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |