Fibonacci word: Difference between revisions

Content added Content deleted
Line 1,824: Line 1,824:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
<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)])


<lang julia>using DataStructures
function fibboword(n::Int64)::Array{String}
entropy(s::AbstractString) = -sum(x -> x / length(s) * log2(x / length(s)), values(counter(s)))
# Initialize the result

r = Array{String}(n)
function fibboword(n::Int64)
# First element
# Initialize the result
r[1] = "0"
r = Array{String}(n)
# If more than 2, set the second element
# First element
if (n ≥ 2)
r[2] = "1"
r[1] = "0"
# If more than 2, set the second element
end
if n ≥ 2 r[2] = "1" end
# Recursively create elements > 3
# Recursively create elements > 3
for i in 3:n
r[i] = r[i - 1] * r[i - 2]
for i in 3:n
r[i] = r[i - 1] * r[i - 2]
end
return r
end
return r
end
end


function testfibbo(n::Int64)
function testfibbo(n::Integer)
fib = fibboword(n)
fib = fibboword(n)
for i in 1:length(fib)
for i in 1:length(fib)
println(i, "\t", length(fib[i]), "\t", entropy(fib[i]))
@printf("%3d%9d%12.6f\n", i, length(fib[i]), entropy(fib[i]))
end
end
return 0
return 0
end
end
</lang>


println(" n\tlength\tentropy")
{{Out}}
<pre>julia> testfibbo(37);
testfibbo(37)</lang>

1 1 -0.0
{{out}}
2 1 -0.0
<pre> n length entropy
3 2 1.0
1 1 -0.000000
4 3 0.91829586
2 1 -0.000000
5 5 0.9709506
3 2 1.000000
6 8 0.954434
4 3 0.918296
7 13 0.9612366
5 5 0.970951
8 21 0.95871186
6 8 0.954434
9 34 0.9596869
7 13 0.961237
10 55 0.959316
8 21 0.958712
11 89 0.95945793
9 34 0.959687
12 144 0.95940375
10 55 0.959316
13 233 0.95942444
11 89 0.959458
14 377 0.95941657
12 144 0.959404
15 610 0.95941955
13 233 0.959424
16 987 0.9594184
14 377 0.959417
17 1597 0.95941883
15 610 0.959420
18 2584 0.95941865
16 987 0.959418
19 4181 0.9594188
17 1597 0.959419
20 6765 0.9594187
18 2584 0.959419
21 10946 0.9594187
19 4181 0.959419
22 17711 0.9594187
20 6765 0.959419
23 28657 0.9594187
21 10946 0.959419
24 46368 0.9594187
22 17711 0.959419
25 75025 0.9594187
23 28657 0.959419
26 121393 0.9594187
24 46368 0.959419
27 196418 0.9594187
25 75025 0.959419
28 317811 0.9594187
26 121393 0.959419
29 514229 0.9594187
27 196418 0.959419
30 832040 0.9594187
28 317811 0.959419
31 1346269 0.9594187
29 514229 0.959419
32 2178309 0.9594187
30 832040 0.959419
33 3524578 0.9594187
31 1346269 0.959419
34 5702887 0.9594187
32 2178309 0.959419
35 9227465 0.9594187
33 3524578 0.959419
36 14930352 0.9594187
34 5702887 0.959419
37 24157817 0.9594187
35 9227465 0.959419
</pre>
36 14930352 0.959419
37 24157817 0.959419</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==