RosettaCodeData/Task/Fibonacci-word/Pluto/fibonacci-word.pluto
2026-04-30 12:34:36 -04:00

36 lines
774 B
Text

local fmt = require "fmt"
local function entropy(s)
local m = {}
for i = 1, #s do
local c = s[i]
local d = m[c]
m[c] = (d) ? d + 1 : 1
end
local hm = 0
for m:keys() as k do
local c = m[k]
hm += c * math.log(c, 2)
end
local l = #s
return math.log(l, 2) - hm / l
end
local function fibword(n)
if n < 2 then return tostring(n) end
local a = "1"
local b = "0"
local i = 3
while i <= n do
local c = b .. a
a, b = b, c
i += 1
end
return b
end
fmt.print("%2s %10s %10s %s", "n", "Length", " Entropy ", "Fib word")
for i = 1, 37 do
local fw = fibword(i)
fmt.print("%2d %10s %0.8f %s", i, fmt.int(#fw), entropy(fw), fmt.abridge(fw, 20))
end