72 lines
1.8 KiB
Text
72 lines
1.8 KiB
Text
local fmt = require "fmt"
|
|
|
|
local function is_esthetic(n, b)
|
|
if n == 0 then return false end
|
|
local i = n % b
|
|
n //= b
|
|
while n > 0 do
|
|
local j = n % b
|
|
if math.abs(i - j) != 1 then return false end
|
|
n //= b
|
|
i = j
|
|
end
|
|
return true
|
|
end
|
|
|
|
local esths = {}
|
|
|
|
local function dfs(n, m, i)
|
|
if n <= i <= m then esths:insert(i) end
|
|
if i == 0 or i > m then return end
|
|
local d = i % 10
|
|
local i1 = i * 10 + d - 1
|
|
local i2 = i1 + 2
|
|
if d == 0 then
|
|
dfs(n, m, i2)
|
|
elseif d == 9 then
|
|
dfs(n, m, i1)
|
|
else
|
|
dfs(n, m, i1)
|
|
dfs(n, m, i2)
|
|
end
|
|
end
|
|
|
|
local function list_esths(n, n2, m, m2, per_line, all)
|
|
esths:clear()
|
|
for i = 0, 9 do dfs(n2, m2, i) end
|
|
local le = #esths
|
|
fmt.print("Base 10: %,s esthetic numbers between %,s and %,s", le, n, m)
|
|
if all then
|
|
local c = 0
|
|
for esths as esth do
|
|
io.write($"{esth} ")
|
|
if (c+1) % per_line == 0 then print() end
|
|
c += 1
|
|
end
|
|
else
|
|
for i = 1, per_line do io.write($"{esths[i]} ") end
|
|
print("\n............\n")
|
|
for i = le - per_line + 1, le do io.write($"{esths[i]} ") end
|
|
end
|
|
print("\n")
|
|
end
|
|
|
|
for b = 2, 16 do
|
|
print($"Base {b}: {4 * b}th to {6 * b}th esthetic numbers:")
|
|
local n = 1
|
|
local c = 0
|
|
while c < 6 * b do
|
|
if is_esthetic(n, b) then
|
|
c += 1
|
|
if c >= 4 * b then io.write($"{fmt.itoa(n, b)} ") end
|
|
end
|
|
n += 1
|
|
end
|
|
print("\n")
|
|
end
|
|
|
|
-- The following all use the obvious range limitations for the numbers in question.
|
|
list_esths(1000, 1010, 9999, 9898, 16, true)
|
|
list_esths(1e8, 101010101, 13 * 1e7, 123456789, 9, true)
|
|
list_esths(1e11, 101010101010, 13 * 1e10, 123456789898, 7, false)
|
|
list_esths(1e14, 101010101010101, 13 * 1e13, 123456789898989, 5, false)
|