Fibonacci word: Difference between revisions
Content added Content deleted
(Added Fōrmulæ solution) |
Not a robot (talk | contribs) (Add CLU) |
||
Line 955: | Line 955: | ||
36 14930352 0,959418728222743 ... |
36 14930352 0,959418728222743 ... |
||
37 24157817 0,959418728222745 ...</pre> |
37 24157817 0,959418728222745 ...</pre> |
||
=={{header|CLU}}== |
|||
<lang clu>% NOTE: when compiling with Portable CLU, |
|||
% this program needs to be merged with 'useful.lib' to get log() |
|||
% |
|||
% pclu -merge $CLUHOME/lib/useful.lib -compile fib_words.clu |
|||
% Yield pairs of (zeroes, ones) for each Fibonacci word |
|||
% We don't generate the whole words, as that would take too much |
|||
% memory. |
|||
fib_words = iter () yields (int,int) |
|||
az: int := 0 ao: int := 1 |
|||
bz: int := 1 bo: int := 0 |
|||
while true do |
|||
yield(az, ao) |
|||
az, ao, bz, bo := bz, bo, az+bz, ao+bo |
|||
end |
|||
end fib_words |
|||
fib_entropy = proc (zeroes, ones: int) returns (real) |
|||
rsize: real := real$i2r(zeroes + ones) |
|||
zeroes_frac: real := real$i2r(zeroes)/rsize |
|||
ones_frac: real := real$i2r(ones)/rsize |
|||
return(-zeroes_frac*log(zeroes_frac)/log(2.0) |
|||
-ones_frac*log(ones_frac)/log(2.0)) |
|||
except when undefined: return(0.0) end |
|||
end fib_entropy |
|||
start_up = proc () |
|||
max = 37 |
|||
po: stream := stream$primary_output() |
|||
stream$putl(po, " # Length Entropy") |
|||
num: int := 0 |
|||
for zeroes, ones: int in fib_words() do |
|||
num := num + 1 |
|||
stream$putright(po, int$unparse(num), 2) |
|||
stream$putright(po, int$unparse(zeroes+ones), 10) |
|||
stream$putright(po, f_form(fib_entropy(zeroes, ones), 1, 6), 10) |
|||
stream$putl(po, "") |
|||
if num=max then break end |
|||
end |
|||
end start_up </lang> |
|||
{{out}} |
|||
<pre> # Length Entropy |
|||
1 1 0.000000 |
|||
2 1 0.000000 |
|||
3 2 1.000000 |
|||
4 3 0.918296 |
|||
5 5 0.970951 |
|||
6 8 0.954434 |
|||
7 13 0.961237 |
|||
8 21 0.958712 |
|||
9 34 0.959687 |
|||
10 55 0.959316 |
|||
11 89 0.959458 |
|||
12 144 0.959404 |
|||
13 233 0.959424 |
|||
14 377 0.959417 |
|||
15 610 0.959419 |
|||
16 987 0.959418 |
|||
17 1597 0.959419 |
|||
18 2584 0.959419 |
|||
19 4181 0.959419 |
|||
20 6765 0.959419 |
|||
21 10946 0.959419 |
|||
22 17711 0.959419 |
|||
23 28657 0.959419 |
|||
24 46368 0.959419 |
|||
25 75025 0.959419 |
|||
26 121393 0.959419 |
|||
27 196418 0.959419 |
|||
28 317811 0.959419 |
|||
29 514229 0.959419 |
|||
30 832040 0.959419 |
|||
31 1346269 0.959419 |
|||
32 2178309 0.959419 |
|||
33 3524578 0.959419 |
|||
34 5702887 0.959419 |
|||
35 9227465 0.959419 |
|||
36 14930352 0.959419 |
|||
37 24157817 0.959419</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |