RosettaCodeData/Task/Entropy/Pluto/entropy.pluto
2026-04-30 12:34:36 -04:00

22 lines
603 B
Text

do -- Shannon entropy - translated from the Lua sample
local function log2 ( x : number ) : number return math.log(x) / math.log(2) end
local function entropy ( X : string ) : number
local N, count, sum, i = X:len(), {}, 0
for char = 1, N do
i = X[char]
if count[i] then
count[i] += 1
else
count[i] = 1
end
end
for n_i, count_i in pairs(count) do
sum += count_i / N * log2(count_i / N)
end
return -sum
end
print( entropy( "1223334444" ) )
end