Fibonacci word: Difference between revisions

No edit summary
Line 2,236:
37 24157817 0.959418728222745
</pre>
 
=={{header|Nim}}==
<lang Nim>import math, strformat, strutils
 
 
func entropy(str: string): float =
## return the entropy of a fibword string.
if str.len <= 1: return 0.0
let strlen = str.len.toFloat
let count0 = str.count('0').toFloat
let count1 = strlen - count0
result = -(count0 / strlen * log2(count0 / strlen) + count1 / strlen * log2(count1 / strlen))
 
 
iterator fibword(): string =
## Yield the successive fibwords.
var a = "1"
var b = "0"
yield a
yield b
while true:
a = b & a
swap a, b
yield b
 
 
when isMainModule:
echo " n length entropy"
echo "————————————————————————————————"
var n = 0
for str in fibword():
inc n
echo fmt"{n:2} {str.len:8} {entropy(str):.16f}"
if n == 37: break</lang>
 
{{out}}
<pre> n length entropy
————————————————————————————————
1 1 0.0000000000000000
2 1 0.0000000000000000
3 2 1.0000000000000000
4 3 0.9182958340544896
5 5 0.9709505944546686
6 8 0.9544340029249651
7 13 0.9612366047228759
8 21 0.9587118829771318
9 34 0.9596868937742169
10 55 0.9593160320543777
11 89 0.9594579158386696
12 144 0.9594037542210230
13 233 0.9594244469559866
14 377 0.9594165437404408
15 610 0.9594195626031441
16 987 0.9594184095152243
17 1597 0.9594188499578098
18 2584 0.9594186817240321
19 4181 0.9594187459836638
20 6765 0.9594187214386755
21 10946 0.9594187308140277
22 17711 0.9594187272329620
23 28657 0.9594187286008073
24 46368 0.9594187280783368
25 75025 0.9594187282779029
26 121393 0.9594187282016754
27 196418 0.9594187282307918
28 317811 0.9594187282196702
29 514229 0.9594187282239184
30 832040 0.9594187282222958
31 1346269 0.9594187282229155
32 2178309 0.9594187282226789
33 3524578 0.9594187282227691
34 5702887 0.9594187282227347
35 9227465 0.9594187282227479
36 14930352 0.9594187282227428
37 24157817 0.9594187282227447</pre>
 
=={{header|Objeck}}==
Anonymous user