How to resolve the algorithm Fibonacci word step by step in the Lua programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fibonacci word step by step in the Lua programming language
Table of Contents
Problem Statement
The Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence as described here:
Perform the above steps for n = 37. You may display the first few but not the larger values of n. {Doing so will get the task's author into trouble with them what be (again!).} Instead, create a table for F_Words 1 to 37 which shows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci word step by step in the Lua programming language
Source code in the lua programming language
-- Return the base two logarithm of x
function log2 (x) return math.log(x) / math.log(2) end
-- Return the Shannon entropy of X
function entropy (X)
local N, count, sum, i = X:len(), {}, 0
for char = 1, N do
i = X:sub(char, char)
if count[i] then
count[i] = count[i] + 1
else
count[i] = 1
end
end
for n_i, count_i in pairs(count) do
sum = sum + count_i / N * log2(count_i / N)
end
return -sum
end
-- Return a table of the first n Fibonacci words
function fibWords (n)
local fw = {1, 0}
while #fw < n do fw[#fw + 1] = fw[#fw] .. fw[#fw - 1] end
return fw
end
-- Main procedure
print("n\tWord length\tEntropy")
for k, v in pairs(fibWords(37)) do
v = tostring(v)
io.write(k .. "\t" .. #v)
if string.len(#v) < 8 then io.write("\t") end
print("\t" .. entropy(v))
end
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the OCaml programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Erlang programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Python programming language
You may also check:How to resolve the algorithm Parse an IP Address step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Active Directory/Connect step by step in the F# programming language