Fibonacci word: Difference between revisions

no edit summary
No edit summary
Line 1,710:
36 14930352 0.959418
37 24157817 0.959418</pre>
 
=={{header|Julia}}==
<lang julia>
# Code for "entropy" taken from https://rosettacode.org/wiki/Entropy#Julia
entropy(s::String)::Float32 = -sum(x -> x * log(2, x), [count(x -> x == c, s) / length(s) for c in unique(s)])
 
function fibboword(n::Int64)::Array{String}
# Initialize the result
r = Array{String}(n)
# First element
r[1] = "0"
# If more than 2, set the second element
if (n ≥ 2)
r[2] = "1"
end
# Recursively create elements > 3
for i in 3:n
r[i] = r[i - 1] * r[i - 2]
end
return r
end
 
function testfibbo(n::Int64)
fib = fibboword(n)
for i in 1:length(fib)
println(i, "\t", length(fib[i]), "\t", entropy(fib[i]))
end
return 0
end
</lang>
 
{{Out}}
<pre>julia> testfibbo(37);
1 1 -0.0
2 1 -0.0
3 2 1.0
4 3 0.91829586
5 5 0.9709506
6 8 0.954434
7 13 0.9612366
8 21 0.95871186
9 34 0.9596869
10 55 0.959316
11 89 0.95945793
12 144 0.95940375
13 233 0.95942444
14 377 0.95941657
15 610 0.95941955
16 987 0.9594184
17 1597 0.95941883
18 2584 0.95941865
19 4181 0.9594188
20 6765 0.9594187
21 10946 0.9594187
22 17711 0.9594187
23 28657 0.9594187
24 46368 0.9594187
25 75025 0.9594187
26 121393 0.9594187
27 196418 0.9594187
28 317811 0.9594187
29 514229 0.9594187
30 832040 0.9594187
31 1346269 0.9594187
32 2178309 0.9594187
33 3524578 0.9594187
34 5702887 0.9594187
35 9227465 0.9594187
36 14930352 0.9594187
37 24157817 0.9594187
</pre>
 
=={{header|Kotlin}}==
Anonymous user