Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Tweak by using the REXX solution observation that other than 0 the numbers must be odd) |
(Added Lua) |
||
Line 643: | Line 643: | ||
1 3 5 15 17 51 85 255 257 273 771 |
1 3 5 15 17 51 85 255 257 273 771 |
||
819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 |
819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 |
||
</pre> |
|||
=={{header|Lua}}== |
|||
<syntaxhighlight lang="lua"> |
|||
do -- find numbers palendromic in bases 2, 4, and 16 |
|||
local function palendromic( n, base ) |
|||
local digits, v = "", n |
|||
while v > 0 do |
|||
local dPos = ( v % base ) + 1 |
|||
digits = digits..string.sub( "0123456789abcdef", dPos, dPos ) |
|||
v = math.floor( v / base ) |
|||
end |
|||
return digits == string.reverse( digits ) |
|||
end |
|||
-- as noted by the REXX sample, all even numbers end in 0 in base 2 |
|||
-- so 0 is the only possible even number, note 0 is palendromic in all bases |
|||
io.write( " 0" ) |
|||
for n = 1, 24999, 2 do |
|||
if palendromic( n, 16 ) then |
|||
if palendromic( n, 4 ) then |
|||
if palendromic( n, 2 ) then |
|||
io.write( " ", n ) |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 |
|||
</pre> |
</pre> |
||