Fibonacci word: Difference between revisions

Added Easylang
(Added Easylang)
 
(35 intermediate revisions by 19 users not shown)
Line 1:
{{task}}
[[File:Fibonacci word cutting sequence.png|thumb|Fibonacci word.]]
 
The   Fibonacci Word   may be created in a manner analogous to the   Fibonacci Sequence   [http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf as described here]:
 
Line 25:
*   [[Entropy/Narcissist]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F entropy(s)
I s.len <= 1
R 0.0
V lns = Float(s.len)
V count0 = s.count(‘0’)
R -sum((count0, s.len - count0).map(count -> count / @lns * log(count / @lns, 2)))
 
V fwords = [String(‘1’), ‘0’]
print(‘#<3 #10 #<10 #.’.format(‘N’, ‘Length’, ‘Entropy’, ‘Fibword’))
L(n) 1..37
L fwords.len < n
fwords [+]= reversed(fwords[(len)-2..]).join(‘’)
V v = fwords[n - 1]
print(‘#3.0 #10.0 #2.7 #.’.format(n, v.len, entropy(v), I v.len < 56 {v} E ‘<too long>’))</syntaxhighlight>
 
{{out}}
<pre>
N Length Entropy Fibword
1 1 0.0000000 1
2 1 0.0000000 0
3 2 1.0000000 01
4 3 0.9182958 010
5 5 0.9709506 01001
6 8 0.9544340 01001010
7 13 0.9612366 0100101001001
8 21 0.9587119 010010100100101001010
9 34 0.9596869 0100101001001010010100100101001001
10 55 0.9593160 0100101001001010010100100101001001010010100100101001010
11 89 0.9594579 <too long>
12 144 0.9594038 <too long>
13 233 0.9594244 <too long>
14 377 0.9594165 <too long>
15 610 0.9594196 <too long>
16 987 0.9594184 <too long>
17 1597 0.9594188 <too long>
18 2584 0.9594187 <too long>
19 4181 0.9594187 <too long>
20 6765 0.9594187 <too long>
21 10946 0.9594187 <too long>
22 17711 0.9594187 <too long>
23 28657 0.9594187 <too long>
24 46368 0.9594187 <too long>
25 75025 0.9594187 <too long>
26 121393 0.9594187 <too long>
27 196418 0.9594187 <too long>
28 317811 0.9594187 <too long>
29 514229 0.9594187 <too long>
30 832040 0.9594187 <too long>
31 1346269 0.9594187 <too long>
32 2178309 0.9594187 <too long>
33 3524578 0.9594187 <too long>
34 5702887 0.9594187 <too long>
35 9227465 0.9594187 <too long>
36 14930352 0.9594187 <too long>
37 24157817 0.9594187 <too long>
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Unbounded,
Ada.Strings.Unbounded.Text_IO, Ada.Numerics.Long_Elementary_Functions,
Ada.Long_Float_Text_IO;
Line 83 ⟶ 143:
end loop;
end Fibonacci_Words;
</syntaxhighlight>
</lang>
Output
<pre> N Length Entropy Word
Line 126 ⟶ 186:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">real
entropy(data b)
{
Line 134 ⟶ 194:
ones = zeros = 0;
 
i = -(count = b_length(~b));
while (i) {
if (b[i] == '0') {
Line 157 ⟶ 217:
b = "0";
 
o_form("%2d %9d /w12p10d10/ ~\n", 1, b_length(~a), 0r, a);
o_form("%2d %9d /w12p10d10/ ~\n", 2, b_length(~b), 0r, b);
i = 3;
while (i <= 37) {
b_stockbu_copy(a, 0, b);
o_form("%2d %9d /w12p10d10/ ~\n", i, b_length(~a), entropy(a),
__hold(i < 10, ? a,.string : ""));
i += 1;
b_swapb.swap(a, b);
}
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1 1 0 1
Line 211 ⟶ 271:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># calculate some details of "Fibonacci Words" #
 
# fibonacci word 1 = "1" #
Line 329 ⟶ 389:
print fibonacci word stats( 37 )
)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 373 ⟶ 433:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
F_WORD←{{⍵,,/⌽¯2↑⍵}⍣(0⌈⍺-2),¨⍵}
ENTROPY←{-+/R×2⍟R←(+⌿⍵∘.=∪⍵)÷⍴⍵}
FORMAT←{'N' 'LENGTH' 'ENTROPY'⍪(⍳⍵),↑{(⍴⍵),ENTROPY ⍵}¨⍵ F_WORD 1 0}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 420 ⟶ 480:
37 24157817 0.9594187282
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">entropy: function [s][
if 1 >= size s -> return 0.0
strlen: to :floating size s
count0: to :floating size match s "0"
count1: strlen - count0
return neg add (count0/strlen) * log count0/strlen 2 (count1/strlen) * log count1/strlen 2
]
 
fibwords: function [n][
x: 0
a: "1"
b: "0"
result: @[a b]
while [x<n][
a: b ++ a
 
tmp: b
b: a
a: tmp
result: result ++ b
x: x+1
]
return result
]
 
loop.with:'i fibwords 37 'w [
print [
pad to :string i+1 4
pad to :string size w 10
pad to :string entropy w 20
]
]</syntaxhighlight>
 
{{out}}
 
<pre> 1 1 0.0
2 1 0.0
3 2 1.0
4 3 0.9182958340544896
5 5 0.9709505944546686
6 8 0.9544340029249649
7 13 0.961236604722876
8 21 0.9587118829771318
9 34 0.9596868937742169
10 55 0.9593160320543777
11 89 0.9594579158386696
12 144 0.959403754221023
13 233 0.9594244469559867
14 377 0.9594165437404407
15 610 0.9594195626031441
16 987 0.9594184095152245
17 1597 0.9594188499578099
18 2584 0.9594186817240321
19 4181 0.9594187459836638
20 6765 0.9594187214386756
21 10946 0.9594187308140278
22 17711 0.959418727232962
23 28657 0.9594187286008073
24 46368 0.9594187280783371
25 75025 0.9594187282779029
26 121393 0.9594187282016755
27 196418 0.9594187282307918
...</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetFormat, FloatFast, 0.15
SetBatchLines, -1
OutPut := "N`tLength`t`tEntropy`n"
Line 449 ⟶ 575:
}
return, e
}</langsyntaxhighlight>
'''Output:'''
<pre>N Length Entropy
Line 491 ⟶ 617:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 587 ⟶ 713:
free(current_word);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> N Length Entropy Word
Line 629 ⟶ 755:
</pre>
 
=={{header|C++ sharp|C#}}==
<syntaxhighlight lang="c sharp">using SYS = System;
<lang Cpp>#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
 
double log2( double number ) {
return ( log( number ) / log( 2 ) ) ;
}
 
double find_entropy( std::string & fiboword ) {
std::map<char , int> frequencies ;
std::for_each( fiboword.begin( ) , fiboword.end( ) ,
[ & frequencies ]( char c ) { frequencies[ c ]++ ; } ) ;
int numlen = fiboword.length( ) ;
double infocontent = 0 ;
for ( std::pair<char , int> p : frequencies ) {
double freq = static_cast<double>( p.second ) / numlen ;
infocontent += freq * log2( freq ) ;
}
infocontent *= -1 ;
return infocontent ;
}
 
void printLine( std::string &fiboword , int n ) {
std::cout << std::setw( 5 ) << std::left << n ;
std::cout << std::setw( 12 ) << std::right << fiboword.size( ) ;
std::cout << " " << std::setw( 16 ) << std::setprecision( 13 )
<< std::left << find_entropy( fiboword ) ;
std::cout << "\n" ;
}
 
int main( ) {
std::cout << std::setw( 5 ) << std::left << "N" ;
std::cout << std::setw( 12 ) << std::right << "length" ;
std::cout << " " << std::setw( 16 ) << std::left << "entropy" ;
std::cout << "\n" ;
std::string firststring ( "1" ) ;
int n = 1 ;
printLine( firststring , n ) ;
std::string secondstring( "0" ) ;
n++ ;
printLine( secondstring , n ) ;
while ( n < 37 ) {
std::string resultstring = firststring + secondstring ;
firststring.assign( secondstring ) ;
secondstring.assign( resultstring ) ;
n++ ;
printLine( resultstring , n ) ;
}
return 0 ;
}</lang>
{{out}}
<pre>N length entropy
1 1 -0
2 1 -0
3 2 1
4 3 0.9182958340545
5 5 0.9709505944547
6 8 0.954434002925
7 13 0.9612366047229
8 21 0.9587118829771
9 34 0.9596868937742
10 55 0.9593160320544
11 89 0.9594579158387
12 144 0.959403754221
13 233 0.959424446956
14 377 0.9594165437404
15 610 0.9594195626031
16 987 0.9594184095152
17 1597 0.9594188499578
18 2584 0.959418681724
19 4181 0.9594187459837
20 6765 0.9594187214387
21 10946 0.959418730814
22 17711 0.959418727233
23 28657 0.9594187286008
24 46368 0.9594187280783
25 75025 0.9594187282779
26 121393 0.9594187282017
27 196418 0.9594187282308
28 317811 0.9594187282197
29 514229 0.9594187282239
30 832040 0.9594187282223
31 1346269 0.9594187282229
32 2178309 0.9594187282227
33 3524578 0.9594187282228
34 5702887 0.9594187282227
35 9227465 0.9594187282227
36 14930352 0.9594187282227
37 24157817 0.9594187282227
</pre>
 
=={{header|C sharp}}==
<lang C sharp>using SYS = System;
using SCG = System.Collections.Generic;
 
Line 795 ⟶ 826:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>N Length Entropy
Line 835 ⟶ 866:
36 14930352 0.959418728222743
37 24157817 0.959418728222745</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
 
double log2( double number ) {
return ( log( number ) / log( 2 ) ) ;
}
 
double find_entropy( std::string & fiboword ) {
std::map<char , int> frequencies ;
std::for_each( fiboword.begin( ) , fiboword.end( ) ,
[ & frequencies ]( char c ) { frequencies[ c ]++ ; } ) ;
int numlen = fiboword.length( ) ;
double infocontent = 0 ;
for ( std::pair<char , int> p : frequencies ) {
double freq = static_cast<double>( p.second ) / numlen ;
infocontent += freq * log2( freq ) ;
}
infocontent *= -1 ;
return infocontent ;
}
 
void printLine( std::string &fiboword , int n ) {
std::cout << std::setw( 5 ) << std::left << n ;
std::cout << std::setw( 12 ) << std::right << fiboword.size( ) ;
std::cout << " " << std::setw( 16 ) << std::setprecision( 13 )
<< std::left << find_entropy( fiboword ) ;
std::cout << "\n" ;
}
 
int main( ) {
std::cout << std::setw( 5 ) << std::left << "N" ;
std::cout << std::setw( 12 ) << std::right << "length" ;
std::cout << " " << std::setw( 16 ) << std::left << "entropy" ;
std::cout << "\n" ;
std::string firststring ( "1" ) ;
int n = 1 ;
printLine( firststring , n ) ;
std::string secondstring( "0" ) ;
n++ ;
printLine( secondstring , n ) ;
while ( n < 37 ) {
std::string resultstring = firststring + secondstring ;
firststring.assign( secondstring ) ;
secondstring.assign( resultstring ) ;
n++ ;
printLine( resultstring , n ) ;
}
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>N length entropy
1 1 -0
2 1 -0
3 2 1
4 3 0.9182958340545
5 5 0.9709505944547
6 8 0.954434002925
7 13 0.9612366047229
8 21 0.9587118829771
9 34 0.9596868937742
10 55 0.9593160320544
11 89 0.9594579158387
12 144 0.959403754221
13 233 0.959424446956
14 377 0.9594165437404
15 610 0.9594195626031
16 987 0.9594184095152
17 1597 0.9594188499578
18 2584 0.959418681724
19 4181 0.9594187459837
20 6765 0.9594187214387
21 10946 0.959418730814
22 17711 0.959418727233
23 28657 0.9594187286008
24 46368 0.9594187280783
25 75025 0.9594187282779
26 121393 0.9594187282017
27 196418 0.9594187282308
28 317811 0.9594187282197
29 514229 0.9594187282239
30 832040 0.9594187282223
31 1346269 0.9594187282229
32 2178309 0.9594187282227
33 3524578 0.9594187282228
34 5702887 0.9594187282227
35 9227465 0.9594187282227
36 14930352 0.9594187282227
37 24157817 0.9594187282227
</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn entropy [s]
(let [len (count s), log-2 (Math/log 2)]
(->> (frequencies s)
Line 854 ⟶ 980:
(doseq [i (range 1 38)
w (take 37 (fibonacci str "1" "0"))]
(printf "%2d %10d %.15f %s%n" i (count w) (entropy w) (if (<= i 8) w "..."))))</langsyntaxhighlight>
 
Output
Line 895 ⟶ 1,021:
36 14930352 0,959418728222743 ...
37 24157817 0,959418728222745 ...</pre>
 
=={{header|CLU}}==
<syntaxhighlight 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 </syntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="lisp">(defun make-fibwords (array)
(loop for i from 0 below 37
for j = "0" then (concatenate 'string j k)
Line 928 ⟶ 1,139:
for n = (aref *fib* i) do
(format t "~2D ~10D ~17,15F ~A~%"
(1+ i) (length n) (entropy n) (string-or-dots n)))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.math, std.string, std.range;
 
real entropy(T)(T[] s) pure nothrow
Line 953 ⟶ 1,164:
s.dup.representation.entropy.abs,
s.length < 25 ? s : "<too long>");
}</langsyntaxhighlight>
{{out}}
<pre> N Length Entropy Fibword
Line 993 ⟶ 1,204:
36 14930352 0.9594187282227428063 <too long>
37 24157817 0.9594187282227447312 <too long></pre>
 
=={{header|Dart}}==
{{trans|Java}}
<syntaxhighlight lang="Dart">
import 'dart:math';
 
class FWord {
String fWord0 = "";
String fWord1 = "";
 
String nextFWord() {
String result;
 
if (fWord1 == "") {
result = "1";
} else if (fWord0 == "") {
result = "0";
} else {
result = fWord1 + fWord0;
}
 
fWord0 = fWord1;
fWord1 = result;
 
return result;
}
 
static double entropy(String source) {
int length = source.length;
var counts = <String, int>{};
double result = 0.0;
 
for (int i = 0; i < length; i++) {
String c = source[i];
 
if (counts.containsKey(c)) {
counts[c] = counts[c] + 1;
} else {
counts[c] = 1;
}
}
 
counts.values.forEach((count) {
double proportion = count / length;
result -= proportion * (log(proportion) / log(2));
});
 
return result;
}
 
static void main() {
FWord fWord = FWord();
 
for (int i = 0; i < 37;) {
String word = fWord.nextFWord();
print("${++i} ${word.length} ${entropy(word)}");
}
}
}
 
void main() {
FWord.main();
}
</syntaxhighlight>
{{out}}
<pre>
1 1 0.0
2 1 0.0
3 2 1.0
4 3 0.9182958340544896
5 5 0.9709505944546686
6 8 0.9544340029249649
7 13 0.961236604722876
8 21 0.9587118829771318
9 34 0.9596868937742169
10 55 0.9593160320543777
11 89 0.9594579158386696
12 144 0.959403754221023
13 233 0.9594244469559867
14 377 0.9594165437404407
15 610 0.9594195626031441
16 987 0.9594184095152245
17 1597 0.9594188499578099
18 2584 0.9594186817240321
19 4181 0.9594187459836638
20 6765 0.9594187214386756
21 10946 0.9594187308140278
22 17711 0.959418727232962
23 28657 0.9594187286008073
24 46368 0.9594187280783371
25 75025 0.9594187282779029
26 121393 0.9594187282016755
27 196418 0.9594187282307918
28 317811 0.9594187282196702
29 514229 0.9594187282239184
30 832040 0.9594187282222959
31 1346269 0.9594187282229156
32 2178309 0.9594187282226789
33 3524578 0.9594187282227691
34 5702887 0.9594187282227347
35 9227465 0.9594187282227479
36 14930352 0.9594187282227429
37 24157817 0.9594187282227448
 
</pre>
 
 
=={{header|Delphi}}==
See [[#Pascal]]
 
=={{header|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
func log2 x .
return log10 x / log10 2
.
func entropy s$ .
l = len s$
if l <= 1
return 0
.
for v$ in strchars s$
cnt0 += if v$ = "0"
.
cnt1 = l - cnt0
return -(cnt0 / l * log2 (cnt0 / l) + cnt1 / l * log2 (cnt1 / l))
.
a$ = ""
b$ = ""
func$ fibword .
if a$ = ""
a$ = "1"
return a$
.
if b$ = ""
b$ = "0"
return b$
.
a$ = b$ & a$
swap a$ b$
return b$
.
numfmt 6 8
print " n length entropy"
print " ——————————————————————"
for n to 37
s$ = fibword
print n & " " & len s$ & " " & entropy s$
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'struct)
(struct FW ( count0 count1 length string)) ;; a fibonacci word
Line 1,022 ⟶ 1,383:
(printf "%3d %10d %24d %a"
i (FW-length fw) (entropy fw) (FW-string fw))))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,063 ⟶ 1,424:
37 24157817 0.9594187282227449 ...
</pre>
 
 
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
{{works with|Erlang/OTP|18}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def entropy(str) do
leng = String.length(str)
Line 1,089 ⟶ 1,449:
str = if len < 60, do: word, else: "<too long>"
:io.format "~3w ~8w ~17.15f ~s~n", [i+1, len, RC.entropy(word), str]
end)</langsyntaxhighlight>
 
{{out}}
Line 1,134 ⟶ 1,494:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">// include the code from /wiki/Entropy#F.23 for the entropy function
 
let fiboword =
Line 1,144 ⟶ 1,504:
Seq.iteri (fun i (s : string) ->
printfn "%3i %10i %10.7g %s" (i+1) s.Length (entropy s) (if s.Length < 40 then s else ""))
(Seq.take 37 fiboword)</langsyntaxhighlight>
{{out}}
<pre> # Length Entropy Word (if length < 40)
Line 1,184 ⟶ 1,544:
36 14930352 0.9594187
37 24157817 0.9594187</pre>
 
=={{header|Factor}}==
It is not necessary to calculate each fibonacci word, since every fibonacci word less than 37 is contained in the 37th fibonacci word. In order to obtain the nth fibonacci word ( <= 37 ), we start with the 37th fibonacci word and take the subsequence from index 0 to the nth fibonacci number, as in the standard fibonacci sequence.
<syntaxhighlight lang="factor">USING: assocs combinators formatting kernel math math.functions
math.ranges math.statistics namespaces pair-rocket sequences ;
IN: rosetta-code.fibonacci-word
 
SYMBOL: 37th-fib-word
 
: fib ( n -- m )
{
1 => [ 1 ]
2 => [ 1 ]
[ [ 1 - fib ] [ 2 - fib ] bi + ]
} case ;
 
: fib-word ( n -- seq )
{
1 => [ "1" ]
2 => [ "0" ]
[ [ 1 - fib-word ] [ 2 - fib-word ] bi append ]
} case ;
 
: nth-fib-word ( n -- seq )
dup 1 =
[ drop "1" ] [ 37th-fib-word get swap fib head ] if ;
: entropy ( seq -- entropy )
[ length ] [ histogram >alist [ second ] map ] bi
[ swap / ] with map
[ dup log 2 log / * ] map-sum
dup 0. = [ neg ] unless ;
 
37 fib-word 37th-fib-word set
"N" "Length" "Entropy" "%2s %8s %10s\n" printf
37 [1,b] [
dup nth-fib-word [ length ] [ entropy ] bi
"%2d %8d %.8f\n" printf
] each</syntaxhighlight>
{{out}}
<pre>
N Length Entropy
1 1 0.00000000
2 1 0.00000000
3 2 1.00000000
4 3 0.91829583
5 5 0.97095059
6 8 0.95443400
7 13 0.96123660
8 21 0.95871188
9 34 0.95968689
10 55 0.95931603
11 89 0.95945792
12 144 0.95940375
13 233 0.95942445
14 377 0.95941654
15 610 0.95941956
16 987 0.95941841
17 1597 0.95941885
18 2584 0.95941868
19 4181 0.95941875
20 6765 0.95941872
21 10946 0.95941873
22 17711 0.95941873
23 28657 0.95941873
24 46368 0.95941873
25 75025 0.95941873
26 121393 0.95941873
27 196418 0.95941873
28 317811 0.95941873
29 514229 0.95941873
30 832040 0.95941873
31 1346269 0.95941873
32 2178309 0.95941873
33 3524578 0.95941873
34 5702887 0.95941873
35 9227465 0.95941873
36 14930352 0.95941873
37 24157817 0.95941873
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 25-06-2015
' compile with: fbc -s console
 
Line 1,245 ⟶ 1,686:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> N Length Entropy Word
Line 1,285 ⟶ 1,726:
36 14930352 0.959418728222743
37 24157817 0.959418728222745</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Fibonacci_word}}
 
'''Solution'''
 
[[File:Fōrmulæ - Fibonacci word 01.png]]
 
'''Example'''
 
[[File:Fōrmulæ - Fibonacci word 02.png]]
 
[[File:Fōrmulæ - Fibonacci word 03.png]]
 
'''Table of Fibonacci word lengths and entropies'''
 
It is not necessary to calculate the actual values for the Fibonacci words in each step.
 
Because the generation of a new word is the concatenation of the two previous words, its length is the sum of the two previous lengths. They are Fibonacci numbers.
 
For the same reason (concatenation), the number of zeros of a new word is the sum of the number of zeros of the two previous words. The same happens to ones.
 
The following table shows the length, the number of zeros and the number of ones of a sequence of Fibonacci words. Notice that all of them are Fibonacci sequences.
 
[[File:Fōrmulæ - Fibonacci word 04.png]]
 
[[File:Fōrmulæ - Fibonacci word 05.png]]
 
Also notice that:
 
* The number of zeros of the i-th word is the length of the (i - 1)-th word.
* The number of ones of the i-th word is the length of the (i - 2)-th word.
 
Since we only need the number of zeros and ones of a given Fibonacci word in order to calculate its entropy, the actual word is not necessary:
 
[[File:Fōrmulæ - Fibonacci word 06.png]]
 
[[File:Fōrmulæ - Fibonacci word 07.png]]
 
'''Limit of the entropy'''
 
Given that the entropy of the i-th Fibonacci word is:
 
<math display="block"> - \left( \frac{zeroes_i}{zeroes_i + ones_i} \lg \left( \frac{zeroes_i}{zeroes_i + ones_i} \right) + \frac{ones_i}{zeroes_i + ones_i} \lg \left( \frac{ones_i}{zeroes_i + ones_i} \right) \right)</math>
 
<math display="block"> - \left( \frac{F_{i-1}}{F_i} \lg \left( \frac{F_{i-1}}{F_i} \right) + \frac{F_{i-2}}{F_i} \lg \left( \frac{F_{i-2}}{F_i} \right) \right)</math>
 
<math display="block"> - \left( \frac{F_{i-1}}{F_i} \lg \left( \frac{F_{i-1}}{F_i} \right) + \frac{F_{i-2}}{F_{i-1}} \frac{F_{i-1}}{F_i} \lg \left( \frac{F_{i-2}}{F_{i-1}} \frac{F_{i-1}}{F_i} \right) \right)</math>
 
Because the ratio between a Fibonacci term over the next term is <math>\frac{1}{\varphi}</math> as i is bigger, the previous expression tends to be:
 
<math display="block"> - \left( \frac{1}{\varphi} \lg \left( \frac{1}{\varphi} \right) + \frac{1}{\varphi} \frac{1}{\varphi} \lg \left( \frac{1}{\varphi} \frac{1}{\varphi} \right) \right)</math>
 
<math display="block"> - \left( \frac{1}{\varphi} \lg \left( \frac{1}{\varphi} \right) + \frac{1}{\varphi^2} \lg \left( \frac{1}{\varphi^2} \right) \right)</math>
 
This value, with a precision of 100 digits is:
 
[[File:Fōrmulæ - Fibonacci word 08.png]]
 
[[File:Fōrmulæ - Fibonacci word 09.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,347 ⟶ 1,849:
fmt.Printf("%3d %9d %.16f\n", n, len(s), entropy(s))
}
}</langsyntaxhighlight>
{{out}}
[http://play.golang.org/p/e1whcGGOU1 Run in the Go Playground.]
Line 1,358 ⟶ 1,860:
=={{header|Haskell}}==
 
<langsyntaxhighlight Haskelllang="haskell">module Main where
 
import Control.Monad
Line 1,382 ⟶ 1,884:
i l (entropy v) (if l > 40 then "..." else v))
[1..38::Int]
(take 37 $ fibonacci "1" "0")</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,389 ⟶ 1,891:
words are shown, while the Fibonacci word length and [[Entropy]] are shown for all 37.
 
<langsyntaxhighlight lang="unicon">procedure main(A)
n := integer(A[1]) | 37
write(right("N",4)," ",right("length",15)," ",left("Entrophy",15)," ",
Line 1,415 ⟶ 1,917:
every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
return h
end</langsyntaxhighlight>
 
Sample run:
Line 1,464 ⟶ 1,966:
=={{header|J}}==
Implementation:
<langsyntaxhighlight Jlang="j">F_Words=: (,<@;@:{~&_1 _2)@]^:(2-~[)&('1';'0')</langsyntaxhighlight>
 
Also, from the [[Entropy#J|entropy]] page we need:
<langsyntaxhighlight Jlang="j">entropy=: +/@:-@(* 2&^.)@(#/.~ % #)</langsyntaxhighlight>
 
Task example:
 
<langsyntaxhighlight Jlang="j"> (,.~#\)(#,entropy)@> F_Words 37
1 1 0
2 1 0
Line 1,508 ⟶ 2,010:
35 9.22747e6 0.959419
36 1.49304e7 0.959419
37 2.41578e7 0.959419</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">import java.util.*;
 
public class FWord {
Line 1,560 ⟶ 2,062:
}
}
}</langsyntaxhighlight>
Output:
<pre> 1 1 0.0
Line 1,602 ⟶ 2,104:
 
=={{header|JavaScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">//makes outputting a table possible in environments
//that don't support console.table()
function console_table(xs) {
Line 1,702 ⟶ 2,204:
}
 
fibWord(37);</langsyntaxhighlight>
Output:
<pre>|N |Length |Entropy |Word |
Line 1,742 ⟶ 2,244:
|36|14930352|0.9594187282227428|... |
|37|24157817|0.9594187282227447|... |</pre>
 
 
=={{header|jq}}==
'''Entropy''':
<langsyntaxhighlight lang="jq"># Input: an array of strings.
# Output: an object with the strings as keys,
# the values of which are the corresponding frequencies.
Line 1,754 ⟶ 2,255:
# entropy in bits of the input string
def entropy:
(explode | map( [.] | implode ) | counter | [ .[] | . * (.|log) ] | add) as $sum
| ((length|log) - ($sum / length)) / (2|log) ;</langsyntaxhighlight>
'''Pretty printing''':
<langsyntaxhighlight lang="jq"># truncate n places after the decimal point;
# return a string since it can readily be converted back to a number
def precision(n):
Line 1,775 ⟶ 2,276:
else rjustify(n)
end ;
</syntaxhighlight>
</lang>
'''The task''':
<syntaxhighlight lang="jq">
<lang jq># Generate the first n terms of the Fibonacci word sequence
def enumerate(s): foreach s as $x (-1; .+1; [., $x]);
# as a stream of arrays of the form [index, word]
 
def fibonacci_words(n):
def fibonacci_words:
# input: [f(i-2), f(i-1), countdown, counter]
"1",
def fib:
(["0","1"]
if .[2] == 1 then [.[3], .[0]]
| recurse([add, .[0]])
else
(.[1] +| .[0]) as $sum;
 
| [ .[3], .[0]], ([ .[1], $sum, (.[2] - 1), (.[3] + 1) ] | fib)
# Generate the first n terms of the Fibonacci word sequence
end;
# as a stream of arrays of the form [index, word] starting with [0,1]
if n <= 0 then empty
def fibonacci_words($n):
else (["1", "0", n, 1] | fib)
enumerate(limit($n; fibonacci_words));
end;
 
def task(n):
Line 1,800 ⟶ 2,301:
 
task(37)
</syntaxhighlight>
</lang>
{{Out}} (head and tail)
<pre>$ jq -n -r -f fibonacci_word.rc
Line 1,826 ⟶ 2,327:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using DataStructures
entropy(s::AbstractString) = -sum(x -> x / length(s) * log2(x / length(s)), values(counter(s)))
 
Line 1,852 ⟶ 2,353:
 
println(" n\tlength\tentropy")
testfibbo(37)</langsyntaxhighlight>
 
{{out}}
Line 1,895 ⟶ 2,396:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun fibWord(n: Int): String {
Line 1,926 ⟶ 2,427:
else println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,972 ⟶ 2,473:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Return the base two logarithm of x
function log2 (x) return math.log(x) / math.log(2) end
Line 2,006 ⟶ 2,507:
if string.len(#v) < 8 then io.write("\t") end
print("\t" .. entropy(v))
end</langsyntaxhighlight>
{{out}}
<pre>n Word length Entropy
Line 2,048 ⟶ 2,549:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">entropy = (p - 1) Log[2, 1 - p] - p Log[2, p];
 
TableForm[
Line 2,054 ⟶ 2,555:
Quiet@Check[N[entropy /. {p -> Fibonacci[k - 1]/Fibonacci[k]}, 15],
0]}, {k, 37}],
TableHeadings -> {None, {"N", "Length", "Entropy"}}]</langsyntaxhighlight>
 
{{out}}<pre>N Length Entropy
Line 2,095 ⟶ 2,596:
37 24157817 0.959418728222745
</pre>
 
=={{header|Nim}}==
<syntaxhighlight 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</syntaxhighlight>
 
{{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}}==
<langsyntaxhighlight lang="objeck">use Collection;
 
class FibonacciWord {
Line 2,116 ⟶ 2,692:
 
length := result->Size();
entropy := 0.0;
 
counts := frequencies->GetValues();
each(i : counts) {
count := counts->Get(i)->As(IntHolder)->Get();
Line 2,154 ⟶ 2,730:
};
}
}</langsyntaxhighlight>
 
Output:
Line 2,199 ⟶ 2,775:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: entropy(s) -- f
| freq sz |
s size dup ifZero: [ return ] asFloat ->sz
Line 2,211 ⟶ 2,787:
ListBuffer new dup add("1") dup add("0") dup ->ws
3 n for: i [ i 1- ws at i 2 - ws at + ws add ]
dup map(#[ dup size swap entropy Pair new]) apply(#println) ;</langsyntaxhighlight>
 
{{out}}
Line 2,257 ⟶ 2,833:
=={{header|ooRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* 09.08.2014 Walter Pachl 'copied' from REXX
* lists the # of chars in fibonacci words and the words' entropy
Line 2,307 ⟶ 2,883:
Return left(1,d+2) /* return a left-justified "1". */
Return format(s,,d) /* normalize the number (sum or S)*/
::requires rxm.cls</langsyntaxhighlight>
{{out}}
<pre> N length Entropy Fibonacci word # of zeroes # of ones
Line 2,362 ⟶ 2,938:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">ent(a,b)=[a,b]=[a,b]/(a+b);(a*log(if(a,a,1))+b*log(if(b,b,1)))/log(1/2)
allocatemem(75<<20) \\ Allocate 75 MB stack space
F=vector(37);F[1]="1";F[2]="0";for(n=3,37,F[n]=Str(F[n-1],F[n-2]))
for(n=1,37,print(n" "fibonacci(n)" "ent(fibonacci(n-1),fibonacci(n-2))))</langsyntaxhighlight>
 
For those output fascists:
Line 2,410 ⟶ 2,986:
{{works with|Freepascal propably Delphi/Turbo-Pascal}}
As in Algol68 statet, you needn't to create the long string.
<langsyntaxhighlight lang="pascal">program FibWord;
{$IFDEF DELPHI}
{$APPTYPE CONSOLE}
Line 2,519 ⟶ 3,095:
end;
END.
</syntaxhighlight>
</lang>
The same output:
<pre>No. Length Entropy Word
Line 2,540 ⟶ 3,116:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub fiboword;
{
my ($a, $b, $count) = (1, 0, 0);
Line 2,570 ⟶ 3,146:
entropy($word),
$count > 9 ? '' : $word
}</langsyntaxhighlight>
=={{header|Perl 6}}==
 
<lang perl6>constant @fib-word = 1, 0, { $^b ~ $^a } ... *;
sub entropy {
-log(2) R/
[+] map -> \p { p * log p },
$^string.comb.Bag.values »/» $string.chars
}
for @fib-word[^37] {
printf "%5d\t%10d\t%.8e\t%s\n",
(state $n)++, .chars, .&entropy, $n > 10 ?? '' !! $_;
}</lang>
 
That works, but is terribly slow due to all the string processing and bag creation, just to count 0's and 1's. By contrast, the following prints the table up to 100 almost instantly by tracking the values to calculate entropy in parallel with the actual strings. This works in Perl 6 because lazy lists are calculated on demand, so if we don't actually ask for the larger string forms, we don't calculate them. Which would be relatively difficult for a string containing 573147844013817084101 characters, unless you happen to have a computer with a zettabyte or so of memory sitting in your garage.
 
<lang perl6>constant @fib-word = '1', '0', { $^b ~ $^a } ... *;
constant @fib-ones = 1, 0, * + * ... *;
constant @fib-chrs = 1, 1, * + * ... *;
multi entropy(0) { 0 }
multi entropy(1) { 0 }
multi entropy($n) {
my $chars = @fib-chrs[$n];
my $ones = @fib-ones[$n];
my $zeros = $chars - $ones;
-log(2) R/
[+] map -> \p { p * log p },
$ones / $chars, $zeros / $chars
}
 
for 0..100 -> $n {
printf "%5d\t%21d\t%.15e\t%s\n",
$n, @fib-chrs[$n], entropy($n), $n > 9 ?? '' !! @fib-word[$n];
}</lang>
{{out}}
<pre> 0 1 0.000000000000000e+00 1
1 1 0.000000000000000e+00 0
2 2 1.000000000000000e+00 01
3 3 9.182958340544895e-01 010
4 5 9.709505944546688e-01 01001
5 8 9.544340029249650e-01 01001010
6 13 9.612366047228759e-01 0100101001001
7 21 9.587118829771317e-01 010010100100101001010
8 34 9.596868937742167e-01 0100101001001010010100100101001001
9 55 9.593160320543776e-01 0100101001001010010100100101001001010010100100101001010
10 89 9.594579158386695e-01
11 144 9.594037542210229e-01
12 233 9.594244469559866e-01
13 377 9.594165437404406e-01
14 610 9.594195626031441e-01
15 987 9.594184095152244e-01
16 1597 9.594188499578099e-01
17 2584 9.594186817240321e-01
18 4181 9.594187459836640e-01
19 6765 9.594187214386754e-01
20 10946 9.594187308140276e-01
21 17711 9.594187272329618e-01
22 28657 9.594187286008074e-01
23 46368 9.594187280783370e-01
24 75025 9.594187282779029e-01
25 121393 9.594187282016755e-01
26 196418 9.594187282307919e-01
27 317811 9.594187282196701e-01
28 514229 9.594187282239183e-01
29 832040 9.594187282222958e-01
30 1346269 9.594187282229156e-01
31 2178309 9.594187282226789e-01
32 3524578 9.594187282227692e-01
33 5702887 9.594187282227345e-01
34 9227465 9.594187282227477e-01
35 14930352 9.594187282227427e-01
36 24157817 9.594187282227447e-01
37 39088169 9.594187282227441e-01
38 63245986 9.594187282227441e-01
39 102334155 9.594187282227441e-01
40 165580141 9.594187282227441e-01
41 267914296 9.594187282227441e-01
42 433494437 9.594187282227441e-01
43 701408733 9.594187282227441e-01
44 1134903170 9.594187282227441e-01
45 1836311903 9.594187282227441e-01
46 2971215073 9.594187282227441e-01
47 4807526976 9.594187282227441e-01
48 7778742049 9.594187282227441e-01
49 12586269025 9.594187282227441e-01
50 20365011074 9.594187282227441e-01
51 32951280099 9.594187282227441e-01
52 53316291173 9.594187282227441e-01
53 86267571272 9.594187282227441e-01
54 139583862445 9.594187282227441e-01
55 225851433717 9.594187282227441e-01
56 365435296162 9.594187282227441e-01
57 591286729879 9.594187282227441e-01
58 956722026041 9.594187282227441e-01
59 1548008755920 9.594187282227441e-01
60 2504730781961 9.594187282227441e-01
61 4052739537881 9.594187282227441e-01
62 6557470319842 9.594187282227441e-01
63 10610209857723 9.594187282227441e-01
64 17167680177565 9.594187282227441e-01
65 27777890035288 9.594187282227441e-01
66 44945570212853 9.594187282227441e-01
67 72723460248141 9.594187282227441e-01
68 117669030460994 9.594187282227441e-01
69 190392490709135 9.594187282227441e-01
70 308061521170129 9.594187282227441e-01
71 498454011879264 9.594187282227441e-01
72 806515533049393 9.594187282227441e-01
73 1304969544928657 9.594187282227441e-01
74 2111485077978050 9.594187282227441e-01
75 3416454622906707 9.594187282227441e-01
76 5527939700884757 9.594187282227441e-01
77 8944394323791464 9.594187282227441e-01
78 14472334024676221 9.594187282227441e-01
79 23416728348467685 9.594187282227441e-01
80 37889062373143906 9.594187282227441e-01
81 61305790721611591 9.594187282227441e-01
82 99194853094755497 9.594187282227441e-01
83 160500643816367088 9.594187282227441e-01
84 259695496911122585 9.594187282227441e-01
85 420196140727489673 9.594187282227441e-01
86 679891637638612258 9.594187282227441e-01
87 1100087778366101931 9.594187282227441e-01
88 1779979416004714189 9.594187282227441e-01
89 2880067194370816120 9.594187282227441e-01
90 4660046610375530309 9.594187282227441e-01
91 7540113804746346429 9.594187282227441e-01
92 12200160415121876738 9.594187282227441e-01
93 19740274219868223167 9.594187282227441e-01
94 31940434634990099905 9.594187282227441e-01
95 51680708854858323072 9.594187282227441e-01
96 83621143489848422977 9.594187282227441e-01
97 135301852344706746049 9.594187282227441e-01
98 218922995834555169026 9.594187282227441e-01
99 354224848179261915075 9.594187282227441e-01
100 573147844013817084101 9.594187282227441e-01</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function log2(atom v)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
return log(v)/log(2)
<span style="color: #008080;">function</span> <span style="color: #000000;">entropy</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">symbols</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">symbols</span><span style="color: #0000FF;">))</span>
function entropy(sequence s)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence symbols = {},
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">symbols</span><span style="color: #0000FF;">)</span>
counts = {}
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
integer N = length(s)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to N do
<span style="color: #004080;">atom</span> <span style="color: #000000;">H</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
object si = s[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer k = find(si,symbols)
<span style="color: #004080;">atom</span> <span style="color: #000000;">ci</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
if k=0 then
<span style="color: #000000;">H</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">ci</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ci</span><span style="color: #0000FF;">)</span>
symbols = append(symbols,si)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
counts = append(counts,1)
<span style="color: #008080;">return</span> <span style="color: #000000;">H</span>
else
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
counts[k] += 1
end if
<span style="color: #004080;">sequence</span> <span style="color: #000000;">F_words</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"1"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">}</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">37</span> <span style="color: #008080;">do</span>
atom H = 0
<span style="color: #000000;">F_words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">F_words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">F_words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">F_words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
integer n = length(counts)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to n do
atom ci = counts[i]/N
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">F_words</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
H -= ci*log2(ci)
<span style="color: #004080;">string</span> <span style="color: #000000;">fi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">F_words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: length %9d, entropy %f %s\n"</span><span style="color: #0000FF;">,</span>
return H
<span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">),</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">),</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span><span style="color: #0000FF;">?</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"..."</span><span style="color: #0000FF;">)})</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<!--</syntaxhighlight>-->
sequence F_words = {"1","0"}
for i=3 to 37 do
F_words = append(F_words,F_words[i-1]&F_words[i-2])
end for
 
for i=1 to length(F_words) do
printf(1,"%2d: length %9d, entropy %f %s\n",
{i,length(F_words[i]),entropy(F_words[i]),
iff(i<10?F_words[i],"...")})
end for</lang>
{{out}}
<pre>
Line 2,765 ⟶ 3,194:
37: length 24157817, entropy 0.959419 ...
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
foreach(N in 1..37)
F = fib(N),
E = entropy(F),
if N <= 10 then
printf("%3d %10d %0.16f %w\n",N,length(F),E,F)
else
printf("%3d %10d %0.16f\n",N,length(F),E)
end
end,
nl.
 
table
fib(1) = "1".
fib(2) = "0".
fib(N) = fib(N-1) ++ fib(N-2).
 
entropy(L) = Entropy =>
Len = L.len,
Occ = new_map(),
foreach(E in L)
Occ.put(E, Occ.get(E,0) + 1)
end,
Entropy = -sum([P2*log2(P2) : _C=P in Occ, P2 = P/Len]).</syntaxhighlight>
 
{{out}}
<pre> 1 1 0.0000000000000000 1
2 1 0.0000000000000000 0
3 2 1.0000000000000000 01
4 3 0.9182958340544896 010
5 5 0.9709505944546686 01001
6 8 0.9544340029249651 01001010
7 13 0.9612366047228759 0100101001001
8 21 0.9587118829771318 010010100100101001010
9 34 0.9596868937742169 0100101001001010010100100101001001
10 55 0.9593160320543777 0100101001001010010100100101001001010010100100101001010
11 89 0.9594579158386696
12 144 0.9594037542210230
13 233 0.9594244469559867
14 377 0.9594165437404407
15 610 0.9594195626031441
16 987 0.9594184095152245
17 1597 0.9594188499578098
18 2584 0.9594186817240320
19 4181 0.9594187459836638
20 6765 0.9594187214386756
21 10946 0.9594187308140277
22 17711 0.9594187272329620
23 28657 0.9594187286008073
24 46368 0.9594187280783371
25 75025 0.9594187282779028
26 121393 0.9594187282016754
27 196418 0.9594187282307918
28 317811 0.9594187282196702
29 514229 0.9594187282239183
30 832040 0.9594187282222957
31 1346269 0.9594187282229155
32 2178309 0.9594187282226788
33 3524578 0.9594187282227691
34 5702887 0.9594187282227347
35 9227465 0.9594187282227479
36 14930352 0.9594187282227428
37 24157817 0.9594187282227447</pre>
 
=={{header|PL/I}}==
 
<lang PL/I>fibword: procedure options (main); /* 9 October 2013 */
{{improve|PL/I| <br><br>The task's requirements are to also show the Fibonacci word's entropy. <br><br>}}
 
<syntaxhighlight lang="pl/i">fibword: procedure options (main); /* 9 October 2013 */
declare (fn, fnp1, fibword) bit (32000) varying;
declare (i, ln, lnp1, lfibword) fixed binary(31);
Line 2,790 ⟶ 3,287:
end;
 
end fibword;</langsyntaxhighlight>
<pre> 1 1 1
2 1 0
Line 2,830 ⟶ 3,327:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">EnableExplicit
Define fwx$, n.i
NewMap uchar.i()
Line 2,864 ⟶ 3,361:
PrintN(" N Length Entropy Word")
For n=1 To 37 : fwx$=fw(n) : RowPrint(Str(n),Str(Len(fwx$)),StrD(ce(fwx$),15),fwx$) : Next
Input()</langsyntaxhighlight>
<pre> N Length Entropy Word
1 1 0.000000000000000 0
Line 2,905 ⟶ 3,402:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> import math
>>> from collections import Counter
>>>
Line 2,962 ⟶ 3,459:
36 14930352 0.9594187 <too long>
37 24157817 0.9594187 <too long>
>>> </langsyntaxhighlight>
 
 
 
=={{header|R}}==
With inspiration from [http://rosettacode.org/wiki/Entropy#R here] for the entropy function:
<langsyntaxhighlight lang="rsplus">entropy <- function(s)
{
if (length(s) > 1)
Line 2,998 ⟶ 3,493:
rownames(ret) <- NULL
return(ret)
}</langsyntaxhighlight>
 
Output:
Line 3,040 ⟶ 3,535:
36 14930352 0.9594187 too long
37 24157817 0.9594187 too long</pre>
 
 
 
=={{header|Racket}}==
Line 3,051 ⟶ 3,544:
 
So to start, a massively generalised version:
<langsyntaxhighlight lang="racket">#lang racket
(provide F-Word gen-F-Word (struct-out f-word) f-word-max-length)
(require "entropy.rkt") ; save Entropy task implementation as "entropy.rkt"
Line 3,112 ⟶ 3,605:
(check-match (F-Word 4) (f-word "010" _ _ _))
(check-match (F-Word 5) (f-word "01001" _ _ _))
(check-match (F-Word 8) (f-word "010010100100101001010" _ _ _)))</langsyntaxhighlight>
Output:
<pre> 1 1 0.000000000000 1
Line 3,154 ⟶ 3,647:
 
And a simpler implementation:
<langsyntaxhighlight lang="racket">#lang racket
 
(define f-word-max-length (make-parameter 80))
Line 3,185 ⟶ 3,678:
(check-match (F-Word 4) (f-word "010" _ _ _))
(check-match (F-Word 5) (f-word "01001" _ _ _))
(check-match (F-Word 8) (f-word "010010100100101001010" _ _ _)))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>constant @fib-word = 1, 0, { $^b ~ $^a } ... *;
sub entropy {
-log(2) R/
[+] map -> \p { p * log p },
$^string.comb.Bag.values »/» $string.chars
}
for @fib-word[^37] {
printf "%5d\t%10d\t%.8e\t%s\n",
(state $n)++, .chars, .&entropy, $n > 10 ?? '' !! $_;
}</syntaxhighlight>
 
That works, but is terribly slow due to all the string processing and bag creation, just to count 0's and 1's. By contrast, the following prints the table up to 100 almost instantly by tracking the values to calculate entropy in parallel with the actual strings. This works in Raku because lazy lists are calculated on demand, so if we don't actually ask for the larger string forms, we don't calculate them. Which would be relatively difficult for a string containing 573147844013817084101 characters, unless you happen to have a computer with a zettabyte or so of memory sitting in your garage.
 
<syntaxhighlight lang="raku" line>constant @fib-word = '1', '0', { $^b ~ $^a } ... *;
constant @fib-ones = 1, 0, * + * ... *;
constant @fib-chrs = 1, 1, * + * ... *;
multi entropy(0) { 0 }
multi entropy(1) { 0 }
multi entropy($n) {
my $chars = @fib-chrs[$n];
my $ones = @fib-ones[$n];
my $zeros = $chars - $ones;
-log(2) R/
[+] map -> \p { p * log p },
$ones / $chars, $zeros / $chars
}
 
for 0..100 -> $n {
printf "%5d\t%21d\t%.15e\t%s\n",
$n, @fib-chrs[$n], entropy($n), $n > 9 ?? '' !! @fib-word[$n];
}</syntaxhighlight>
{{out}}
<pre> 0 1 0.000000000000000e+00 1
1 1 0.000000000000000e+00 0
2 2 1.000000000000000e+00 01
3 3 9.182958340544895e-01 010
4 5 9.709505944546688e-01 01001
5 8 9.544340029249650e-01 01001010
6 13 9.612366047228759e-01 0100101001001
7 21 9.587118829771317e-01 010010100100101001010
8 34 9.596868937742167e-01 0100101001001010010100100101001001
9 55 9.593160320543776e-01 0100101001001010010100100101001001010010100100101001010
10 89 9.594579158386695e-01
11 144 9.594037542210229e-01
12 233 9.594244469559866e-01
13 377 9.594165437404406e-01
14 610 9.594195626031441e-01
15 987 9.594184095152244e-01
16 1597 9.594188499578099e-01
17 2584 9.594186817240321e-01
18 4181 9.594187459836640e-01
19 6765 9.594187214386754e-01
20 10946 9.594187308140276e-01
21 17711 9.594187272329618e-01
22 28657 9.594187286008074e-01
23 46368 9.594187280783370e-01
24 75025 9.594187282779029e-01
25 121393 9.594187282016755e-01
26 196418 9.594187282307919e-01
27 317811 9.594187282196701e-01
28 514229 9.594187282239183e-01
29 832040 9.594187282222958e-01
30 1346269 9.594187282229156e-01
31 2178309 9.594187282226789e-01
32 3524578 9.594187282227692e-01
33 5702887 9.594187282227345e-01
34 9227465 9.594187282227477e-01
35 14930352 9.594187282227427e-01
36 24157817 9.594187282227447e-01
37 39088169 9.594187282227441e-01
38 63245986 9.594187282227441e-01
39 102334155 9.594187282227441e-01
40 165580141 9.594187282227441e-01
41 267914296 9.594187282227441e-01
42 433494437 9.594187282227441e-01
43 701408733 9.594187282227441e-01
44 1134903170 9.594187282227441e-01
45 1836311903 9.594187282227441e-01
46 2971215073 9.594187282227441e-01
47 4807526976 9.594187282227441e-01
48 7778742049 9.594187282227441e-01
49 12586269025 9.594187282227441e-01
50 20365011074 9.594187282227441e-01
51 32951280099 9.594187282227441e-01
52 53316291173 9.594187282227441e-01
53 86267571272 9.594187282227441e-01
54 139583862445 9.594187282227441e-01
55 225851433717 9.594187282227441e-01
56 365435296162 9.594187282227441e-01
57 591286729879 9.594187282227441e-01
58 956722026041 9.594187282227441e-01
59 1548008755920 9.594187282227441e-01
60 2504730781961 9.594187282227441e-01
61 4052739537881 9.594187282227441e-01
62 6557470319842 9.594187282227441e-01
63 10610209857723 9.594187282227441e-01
64 17167680177565 9.594187282227441e-01
65 27777890035288 9.594187282227441e-01
66 44945570212853 9.594187282227441e-01
67 72723460248141 9.594187282227441e-01
68 117669030460994 9.594187282227441e-01
69 190392490709135 9.594187282227441e-01
70 308061521170129 9.594187282227441e-01
71 498454011879264 9.594187282227441e-01
72 806515533049393 9.594187282227441e-01
73 1304969544928657 9.594187282227441e-01
74 2111485077978050 9.594187282227441e-01
75 3416454622906707 9.594187282227441e-01
76 5527939700884757 9.594187282227441e-01
77 8944394323791464 9.594187282227441e-01
78 14472334024676221 9.594187282227441e-01
79 23416728348467685 9.594187282227441e-01
80 37889062373143906 9.594187282227441e-01
81 61305790721611591 9.594187282227441e-01
82 99194853094755497 9.594187282227441e-01
83 160500643816367088 9.594187282227441e-01
84 259695496911122585 9.594187282227441e-01
85 420196140727489673 9.594187282227441e-01
86 679891637638612258 9.594187282227441e-01
87 1100087778366101931 9.594187282227441e-01
88 1779979416004714189 9.594187282227441e-01
89 2880067194370816120 9.594187282227441e-01
90 4660046610375530309 9.594187282227441e-01
91 7540113804746346429 9.594187282227441e-01
92 12200160415121876738 9.594187282227441e-01
93 19740274219868223167 9.594187282227441e-01
94 31940434634990099905 9.594187282227441e-01
95 51680708854858323072 9.594187282227441e-01
96 83621143489848422977 9.594187282227441e-01
97 135301852344706746049 9.594187282227441e-01
98 218922995834555169026 9.594187282227441e-01
99 354224848179261915075 9.594187282227441e-01
100 573147844013817084101 9.594187282227441e-01</pre>
 
=={{header|REXX}}==
Programming note: &nbsp; 32-bit Regina REXX (under Windows/XP) can execute this program with &nbsp; N='''42''' &nbsp; without exhausting system resources, &nbsp; the 64-bit version of Regina can calculate bigger Fibonacci words.
<langsyntaxhighlight lang="rexx">/*REXX program displays the number of chars in a fibonacci word, and the word's entropy.*/
d=20 21; de= d + 6; numeric digits de /*use more precision (the default is 9)*/
parse arg N . /*get optional argument from the C.L. */
if N=='' | N=="," then N=42 42 /*Not specified? Then use the default.*/
say center('N', 53) center("length", 12de) center('entropy', de) center("Fib word", 56)
say copies('─', 53) copies("─" , 12de) copies('─' , de) copies("─" , 56)
c= 1 /*initialize [↓]the 1st displayvalue for N fibonacci wordsentropy. */
do j=1 for N; if j==2 then c=0 /*test for[↓] the casedisplay of JN equals fibonacci 2words. */
if j==32 then parse value 1c= 0 with a b /* " " " " " /*test "for the case of " J equals 32. */
if j>2==3 then c=bparse ||value a; 1 L=length(c) 0 with a b /*calculate the FIBword" if we" need to " " " " " 3. */
if j>2 then c= b || a /*calculate the FIBword if we need to.*/
L= length(c) /*find the length of the fib─word C. */
if L<56 then Fw= c
else Fw= '{the word is too wide to display, length is: ' L"}"'
say right(j,4 2) right( commas(L),12 de) ' ' entropy() " " Fw
a= b; b=c c /*define the new values for A and B.*/
end /*j*/ /*display text msg; */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
entropy: if L==1 then return left(0, d+2) /*handle special case of one character.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
!.0=length( space( translate(c,,1), 0)) /*efficient way to count the "zeroes".*/
entropy: if L==1 then return left(0, d + 2) /*handle special case of one character.*/
!.1=L-!.0; $=0; do i=1 for 2; _=i-1 /*construct character from the ether. */
$!.0=$ -!._/L*log2length(space(!._/Ltranslate(c,, 1), 0)) /*addefficient way (negatively)to count the entropies "zeroes". */
!.1= L - !.0; end $= 0 /*idefine 1st fib─word; initial entropy.*/
do i=1 for 2; _= i - 1 /*construct character from the ether. */
$= $ - !._ / L * log2(!._ / L) /*add (negatively) the entropies. */
end /*i*/
if $=1 then return left(1, d+2) /*return a left─justified "1" (one). */
return format($, , d) /*normalize the sum (S) number. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
log2: procedure; parse arg x 1 xx; ig=x>1.5; is=1-2*(ig\==1); numeric digits 5+digits()
Line 3,220 ⟶ 3,857:
xx=izz; m=m+is*2**j; end /*while*/; x=x* e** -m -1; z=0; _=-1; p=z
do k=1; _=-_*x; z=z+_/k; if z=p then leave; p=z; end /*k*/
r=z+m; if arg()==2 then return r; return r / log2(2,.)</langsyntaxhighlight>
'''{{out|output''' |text=&nbsp; forwhen using the first 42 Fibonaccidefault wordsinput:}}
<pre>
N N length entropy Fib word
──────── ─────────────────────────────────────── ───────────────────────────────────────────────────── ────────────────────────────────────────────────────────
1 1 1 0 1
2 2 1 0 0
3 3 2 1 01
4 4 3 0.91829583405448951479918295834054489514787 010
5 5 5 0.97095059445466863900970950594454668638998 01001
6 6 8 0.95443400292496496454954434002924964964536 01001010
7 7 13 0.96123660472287587275961236604722875872754 0100101001001
8 8 21 0.95871188297713180865958711882977131808650 010010100100101001010
9 9 34 0.95968689377421693320959686893774216933196 0100101001001010010100100101001001
10 10 55 0.95931603205437767775959316032054377677752 0100101001001010010100100101001001010010100100101001010
11 11 89 0.95945791583866946166959457915838669461656 {the word is too wide to display, length is: 89}
12 12 144 0.95940375422102292947959403754221022929465 {the word is too wide to display, length is: 144}
13 13 233 0.95942444695598675869959424446955986758690 {the word is too wide to display, length is: 233}
14 14 377 0.95941654374044073872959416543740440738719 {the word is too wide to display, length is: 377}
15 15 610 0.95941956260314415023959419562603144150234 {the word is too wide to display, length is: 610}
16 16 987 0.95941840951522431271959418409515224312708 {the word is too wide to display, length is: 987}
17 17 1597 1,597 0.95941884995780985568959418849957809855676 {the word is too wide to display, length is: 1597}
18 18 2584 2,584 0.95941868172403210665959418681724032106650 {the word is too wide to display, length is: 2584}
19 19 4181 4,181 0.95941874598366381433959418745983663814327 {the word is too wide to display, length is: 4181}
20 20 6765 6,765 0.95941872143867541464959418721438675414636 {the word is too wide to display, length is: 6765}
21 21 10946 10,946 0.95941873081402772313959418730814027723133 {the word is too wide to display, length is: 10946}
22 22 17711 17,711 0.95941872723296194271959418727232961942711 {the word is too wide to display, length is: 17711}
23 23 28657 28,657 0.95941872860080737603959418728600807376027 {the word is too wide to display, length is: 28657}
24 24 46368 46,368 0.95941872807833691494959418728078336914941 {the word is too wide to display, length is: 46368}
25 25 75025 75,025 0.95941872827790287341959418728277902873408 {the word is too wide to display, length is: 75025}
26 26 121393 121,393 0.95941872820167546034959418728201675460337 {the word is too wide to display, length is: 121393}
27 27 196418 196,418 0.95941872823079174127959418728230791741265 {the word is too wide to display, length is: 196418}
28 28 317811 317,811 0.95941872821967031158959418728219670311578 {the word is too wide to display, length is: 317811}
29 29 514229 514,229 0.95941872822391831972959418728223918319715 {the word is too wide to display, length is: 514229}
30 30 832040 832,040 0.95941872822229572499959418728222295724991 {the word is too wide to display, length is: 832040}
31 31 1346269 1,346,269 0.95941872822291550103959418728222915501026 {the word is too wide to display, length is: 1346269}
32 32 2178309 2,178,309 0.95941872822267876765959418728222678767646 {the word is too wide to display, length is: 2178309}
33 33 3524578 3,524,578 0.95941872822276919175959418728222769191751 {the word is too wide to display, length is: 3524578}
34 34 5702887 5,702,887 0.95941872822273465282959418728222734652816 {the word is too wide to display, length is: 5702887}
35 35 9227465 9,227,465 0.95941872822274784552959418728222747845515 {the word is too wide to display, length is: 9227465}
36 36 14930352 14,930,352 0.95941872822274280635959418728222742806353 {the word is too wide to display, length is: 14930352}
37 37 24157817 24,157,817 0.95941872822274473114959418728222744731141 {the word is too wide to display, length is: 24157817}
38 38 39088169 39,088,169 0.95941872822274399594959418728222743995937 {the word is too wide to display, length is: 39088169}
39 39 63245986 63,245,986 0.95941872822274427676959418728222744276760 {the word is too wide to display, length is: 63245986}
40 40 102334155 102,334,155 0.95941872822274416950959418728222744169496 {the word is too wide to display, length is: 102334155}
41 41 165580141 165,580,141 0.95941872822274421047959418728222744210467 {the word is too wide to display, length is: 165580141}
42 42 267914296 267,914,296 0.95941872822274419482959418728222744194817 {the word is too wide to display, length is:} 267914296}
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Fibonacci word
# Date : 2018/01/23
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
 
fw1 = "1"
Line 3,325 ⟶ 3,959:
logBase =log( x) /log( 2)
return logBase
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,372 ⟶ 4,006:
Includes code for entropy from [[Entropy#Ruby|Entropy]] page.
 
<langsyntaxhighlight lang="ruby">#encoding: ASCII-8BIT
 
def entropy(s)
Line 3,395 ⟶ 4,029:
words.each.with_index(1) do |word, i|
puts '%3i %9i %15.12f %s' % [i, word.length, entropy(word), word.length<60 ? word : '<too long>']
end</langsyntaxhighlight>
 
{{out}}
Line 3,442 ⟶ 4,076:
This is not implemented in any sort of generic way and is probably fairly inefficient.
 
<langsyntaxhighlight lang="rust">struct Fib<T> {
curr: T,
next: T,
Line 3,487 ⟶ 4,121:
println!("{:10} {:10} {:.10} {:60}", i + 1, s.len(), get_entropy(&s.bytes().collect::<Vec<_>>()), word);
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,533 ⟶ 4,167:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
//word iterator
def fibIt = Iterator.iterate(("1","0")){case (f1,f2) => (f2,f1+f2)}.map(_._1)
Line 3,551 ⟶ 4,185:
val it = fibIt.zipWithIndex.map(w => (w._2, w._1.length, entropy(w._1)))
println(it.take(37).map{case (n,l,e) => s"$n).\t$l \t$e"}.mkString("\n"))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,596 ⟶ 4,230:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme inexact)
Line 3,639 ⟶ 4,273:
(entropy (vector-ref *words* i)))
"\n")))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,687 ⟶ 4,321:
 
===Recursive function===
<syntaxhighlight lang="text">exec('.\entropy.sci',0);
 
function word=fiboword(n)
Line 3,717 ⟶ 4,351:
 
disp('EXECUTION TIME: '+string(time)+'s.');
disp(['N', 'LENGTH', 'ENTROPY'; string([N char_length entropies])]);</langsyntaxhighlight>
 
{{out}}
Line 3,799 ⟶ 4,433:
 
===Iterative method===
<syntaxhighlight lang="text">exec('.\entropy.sci',0);
 
final_length = 37;
Line 3,831 ⟶ 4,465:
 
disp('EXECUTION TIME: '+string(time)+'s.');
disp(['N', 'LENGTH', 'ENTROPY'; string([N word_length entropies])]);</langsyntaxhighlight>
 
{{out}}
Line 3,914 ⟶ 4,548:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 3,966 ⟶ 4,600:
writeln(index lpad 2 <& length(fibWord) lpad 10 <& " " <& entropy(fibWord) digits 15);
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,009 ⟶ 4,643:
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program fibonacci_words;
print("N Length Entropy");
print("---- ---------- -------------------");
loop for n in [1..37] do
[zeroes, ones] := fibword := fib_word n;
length := zeroes + ones;
print(lpad(str n,4) + " " + lpad(str length,10) + " " + str entropy fibword);
end loop;
 
$ Return the amount of zeroes and ones in the N'th fibonacci word
op fib_word(n);
[a0, a1, b0, b1] := [0, 1, 1, 0];
loop for i in [2..n] do
[a0, a1, b0, b1] := [b0, b1, a0+b0, a1+b1];
end loop;
return [a0, a1];
end op;
 
op entropy(fibword);
[zeroes, ones] := fibword;
fzeroes := zeroes / (zeroes + ones);
fones := ones / (zeroes + ones);
 
if fzeroes = 0 or fones = 0 then
return 0;
end if;
 
return -fzeroes*log fzeroes/log 2 - fones*log fones/log 2;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>N Length Entropy
---- ---------- -------------------
1 1 0
2 1 0
3 2 1
4 3 0.91829583405449
5 5 0.970950594454669
6 8 0.954434002924965
7 13 0.961236604722876
8 21 0.958711882977132
9 34 0.959686893774217
10 55 0.959316032054378
11 89 0.95945791583867
12 144 0.959403754221023
13 233 0.959424446955987
14 377 0.959416543740441
15 610 0.959419562603144
16 987 0.959418409515225
17 1597 0.95941884995781
18 2584 0.959418681724032
19 4181 0.959418745983664
20 6765 0.959418721438675
21 10946 0.959418730814028
22 17711 0.959418727232962
23 28657 0.959418728600807
24 46368 0.959418728078337
25 75025 0.959418728277903
26 121393 0.959418728201676
27 196418 0.959418728230792
28 317811 0.95941872821967
29 514229 0.959418728223918
30 832040 0.959418728222296
31 1346269 0.959418728222916
32 2178309 0.959418728222679
33 3524578 0.959418728222769
34 5702887 0.959418728222735
35 9227465 0.959418728222748
36 14930352 0.959418728222743
37 24157817 0.959418728222745</pre>
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func entropy(s) {
[0] + (s.chars.freq.values »/» s.len) -> reduce { |a,b|
a - b*b.log2
Line 4,032 ⟶ 4,737:
entropy(word),
word.len<30 ? word : '<too long>'))
}</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">import Foundation
 
struct Fib: Sequence, IteratorProtocol {
private var cur: String
private var nex: String
 
init(cur: String, nex: String) {
self.cur = cur
self.nex = nex
}
 
mutating func next() -> String? {
let ret = cur
 
cur = nex
nex = "\(ret)\(nex)"
 
return ret
}
}
 
func getEntropy(_ s: [Int]) -> Double {
var entropy = 0.0
var hist = Array(repeating: 0.0, count: 256)
 
for i in 0..<s.count {
hist[s[i]] += 1
}
 
for i in 0..<256 where hist[i] > 0 {
let rat = hist[i] / Double(s.count)
entropy -= rat * log2(rat)
}
 
return entropy
}
 
for (i, str) in Fib(cur: "1", nex: "0").prefix(37).enumerated() {
let ent = getEntropy(str.map({ Int($0.asciiValue!) }))
 
print("i: \(i) len: \(str.count) entropy: \(ent)")
}</syntaxhighlight>
 
{{out}}
 
<pre style="overflow: scroll; height: 25em">i: 0 len: 1 entropy: 0.0
i: 1 len: 1 entropy: 0.0
i: 2 len: 2 entropy: 1.0
i: 3 len: 3 entropy: 0.9182958340544896
i: 4 len: 5 entropy: 0.9709505944546686
i: 5 len: 8 entropy: 0.954434002924965
i: 6 len: 13 entropy: 0.9612366047228759
i: 7 len: 21 entropy: 0.9587118829771318
i: 8 len: 34 entropy: 0.9596868937742169
i: 9 len: 55 entropy: 0.9593160320543777
i: 10 len: 89 entropy: 0.9594579158386696
i: 11 len: 144 entropy: 0.959403754221023
i: 12 len: 233 entropy: 0.9594244469559866
i: 13 len: 377 entropy: 0.9594165437404408
i: 14 len: 610 entropy: 0.9594195626031441
i: 15 len: 987 entropy: 0.9594184095152243
i: 16 len: 1597 entropy: 0.9594188499578098
i: 17 len: 2584 entropy: 0.9594186817240321
i: 18 len: 4181 entropy: 0.9594187459836638
i: 19 len: 6765 entropy: 0.9594187214386755
i: 20 len: 10946 entropy: 0.9594187308140277
i: 21 len: 17711 entropy: 0.959418727232962
i: 22 len: 28657 entropy: 0.9594187286008073
i: 23 len: 46368 entropy: 0.9594187280783368
i: 24 len: 75025 entropy: 0.9594187282779029
i: 25 len: 121393 entropy: 0.9594187282016754
i: 26 len: 196418 entropy: 0.9594187282307918
i: 27 len: 317811 entropy: 0.9594187282196702
i: 28 len: 514229 entropy: 0.9594187282239184
i: 29 len: 832040 entropy: 0.9594187282222958
i: 30 len: 1346269 entropy: 0.9594187282229155
i: 31 len: 2178309 entropy: 0.9594187282226789
i: 32 len: 3524578 entropy: 0.9594187282227691
i: 33 len: 5702887 entropy: 0.9594187282227347
i: 34 len: 9227465 entropy: 0.9594187282227479
i: 35 len: 14930352 entropy: 0.9594187282227428
i: 36 len: 24157817 entropy: 0.9594187282227447</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc fibwords {n} {
set fw {1 0}
while {[llength $fw] < $n} {
Line 4,062 ⟶ 4,852:
foreach word [fibwords 37] {
fibwordinfo [incr i] $word
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,103 ⟶ 4,893:
36 14930352 0.959418728222743 <too long>
37 24157817 0.959418728222745 <too long>
</pre>
 
=={{header|Uiua}}==
Memoisation saves the day :-)
 
<syntaxhighlight lang="Uiua">
# Build the string recursively.
F ← |1 memo⟨⟨⊂∩F-1.-1|"0"◌⟩=2.|"1"◌⟩=1.
# General entropy formula - quite slow for this task.
Egen ← /+(¯×ₙ2.)÷/+.≡(⧻⊚=)⊃◴¤
# Specific entropy formula for a binary string.
E ← ⍥(0◌)=NaN.+∩(¯×ₙ2.)⟜(¯-1)÷⊃⧻(⧻⊚="1")
 
# Much faster approach -- don't even build the string, just count
# how many "0"s and "1"s the string will have.
Fx ← |1 memo⟨⟨+∩Fx-1.-1|[1 0]◌⟩=2.|[0 1]◌⟩=1.
Ex ← ⍥(0◌)=NaN./+(¯×ₙ2.)÷/+.
 
# Print and time it
⍜now(≡(⇌[⊃/+ (⍜(×1e8)⁅Ex)Fx.])+1⇡37)
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ 1 0 1
2 0 1
3 1 2
4 0.91829583 3
5 0.97095059 5
6 0.954434 8
7 0.9612366 13
8 0.95871188 21
9 0.95968689 34
10 0.95931603 55
...etc...
35 0.95941873 9227465
36 0.95941873 14930352
37 0.95941873 24157817
0.0029999999678694-ε
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var entropy = Fn.new { |s|
var m = {}
for (c in s) {
var d = m[c]
m[c] = (d) ? d + 1 : 1
}
var hm = 0
for (k in m.keys) {
var c = m[k]
hm = hm + c * c.log2
}
var l = s.count
return l.log2 - hm/l
}
 
var fibWord = Fn.new { |n|
if (n < 2) return n.toString
var a = "1"
var b = "0"
var i = 3
while (i <= n) {
var c = b + a
a = b
b = c
i = i + 1
}
return b
}
 
Fmt.print("$2s $10s $10m $s", "n", "Length", "Entropy", "Fib word")
for (i in 1..37) {
var fw = fibWord.call(i)
if (i < 10) {
Fmt.print("$2d $,10d $0.8f $s", i, fw.count, entropy.call(fw), fw)
} else {
Fmt.print("$2d $,10d $0.8f $s", i, fw.count, entropy.call(fw), Fmt.abbreviate(20, fw))
}
}</syntaxhighlight>
 
{{out}}
<pre>
n Length Entropy Fib word
1 1 0.00000000 1
2 1 0.00000000 0
3 2 1.00000000 01
4 3 0.91829583 010
5 5 0.97095059 01001
6 8 0.95443400 01001010
7 13 0.96123660 0100101001001
8 21 0.95871188 010010100100101001010
9 34 0.95968689 0100101001001010010100100101001001
10 55 0.95931603 01001010010010100101...10010100100101001010
11 89 0.95945792 01001010010010100101...10010100100101001001
12 144 0.95940375 01001010010010100101...10010100100101001010
13 233 0.95942445 01001010010010100101...10010100100101001001
14 377 0.95941654 01001010010010100101...10010100100101001010
15 610 0.95941956 01001010010010100101...10010100100101001001
16 987 0.95941841 01001010010010100101...10010100100101001010
17 1,597 0.95941885 01001010010010100101...10010100100101001001
18 2,584 0.95941868 01001010010010100101...10010100100101001010
19 4,181 0.95941875 01001010010010100101...10010100100101001001
20 6,765 0.95941872 01001010010010100101...10010100100101001010
21 10,946 0.95941873 01001010010010100101...10010100100101001001
22 17,711 0.95941873 01001010010010100101...10010100100101001010
23 28,657 0.95941873 01001010010010100101...10010100100101001001
24 46,368 0.95941873 01001010010010100101...10010100100101001010
25 75,025 0.95941873 01001010010010100101...10010100100101001001
26 121,393 0.95941873 01001010010010100101...10010100100101001010
27 196,418 0.95941873 01001010010010100101...10010100100101001001
28 317,811 0.95941873 01001010010010100101...10010100100101001010
29 514,229 0.95941873 01001010010010100101...10010100100101001001
30 832,040 0.95941873 01001010010010100101...10010100100101001010
31 1,346,269 0.95941873 01001010010010100101...10010100100101001001
32 2,178,309 0.95941873 01001010010010100101...10010100100101001010
33 3,524,578 0.95941873 01001010010010100101...10010100100101001001
34 5,702,887 0.95941873 01001010010010100101...10010100100101001010
35 9,227,465 0.95941873 01001010010010100101...10010100100101001001
36 14,930,352 0.95941873 01001010010010100101...10010100100101001010
37 24,157,817 0.95941873 01001010010010100101...10010100100101001001
</pre>
 
Line 4,108 ⟶ 5,023:
{{trans|D}}
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn entropy(bs){ //binary String-->Float
len:=bs.len(); num1s:=(bs-"0").len();
T(num1s,len-num1s).filter().apply('wrap(p){ p=p.toFloat()/len; -p*p.log() })
Line 4,121 ⟶ 5,036:
"%3d %10d %2.10f %s".fmt(n,w.len(),entropy(w),
w.len()<50 and w or "<too long>").println();
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,142 ⟶ 5,057:
=={{header|ZX Spectrum Basic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="zxbasic">10 LET x$="1": LET y$="0": LET z$=""
20 PRINT "N, Length, Entropy, Word"
30 LET n=1
Line 4,176 ⟶ 5,091:
1070 IF t(j)>0 THEN LET prop=t(j)/sourcelen: LET entropy=entropy-(prop*LN (prop)/LN (base))
1080 NEXT j
1090 RETURN</langsyntaxhighlight>
1,983

edits