56 lines
1.5 KiB
Text
56 lines
1.5 KiB
Text
require "table2"
|
|
require "perm"
|
|
|
|
local limit = 1_000_000
|
|
|
|
local function self_ref_seq(s)
|
|
local sb = ""
|
|
for "9876543210":split("") as d do
|
|
if d in s then
|
|
local count = s:split(""):count(|c| -> c == d)
|
|
sb ..= $"{count}{d}"
|
|
end
|
|
end
|
|
return sb
|
|
end
|
|
|
|
local sieve = table.rep(limit, 0)
|
|
local elements = {}
|
|
for n = 1, limit - 1 do
|
|
if sieve[n + 1] == 0 then
|
|
elements:clear()
|
|
local next = tostring(n)
|
|
elements:insert(next)
|
|
while true do
|
|
next = self_ref_seq(next)
|
|
if next in elements then
|
|
local size = #elements
|
|
sieve[n + 1] = size
|
|
if n > 9 then
|
|
local perms = perm.list(tostring(n):split("")):map(|p| -> p:concat(""))
|
|
perms = perms:dedup():reorder()
|
|
for perms as perm do
|
|
if perm[1] != "0" then
|
|
local k = tonumber(perm)
|
|
sieve[k + 1] = size
|
|
end
|
|
end
|
|
end
|
|
break
|
|
end
|
|
elements:insert(next)
|
|
end
|
|
end
|
|
end
|
|
local max_iterations = sieve:max()
|
|
for n = 1, limit - 1 do
|
|
if sieve[n + 1] >= max_iterations then
|
|
print($"{n} -> Iterations = {max_iterations}")
|
|
local next = tostring(n)
|
|
for i = 1, max_iterations do
|
|
print(next)
|
|
next = self_ref_seq(next)
|
|
end
|
|
print()
|
|
end
|
|
end
|