Fibonacci word: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{task}} The standard Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence: : Define F_Word 1 as 0; : Define F_Word 2 as 01; : Form F_Word 3 as F_Wor...")
 
No edit summary
Line 10: Line 10:


Instead create a table which for F_Words 1 to 36 shows the [[Rep-string]] and [[Entropy]].
Instead create a table which for F_Words 1 to 36 shows the [[Rep-string]] and [[Entropy]].

=={{header|Icon}} and {{header|Unicon}}==

The following solution works in both Icon and Unicon. The first 7 Fibonacci
words are shown, while the [[Entropy]] is shown for all 36. None of the fibonacci words
have rep-strings, by the definition given in the [[Rep-string]] task.

<lang unicon>procedure main(A)
n := integer(A[1]) | 36
every w := fword(i := 1 to n) do {
writes(right(i,4)," ",left(H(w),5))
if i <= 7 then write(": ",w) else write()
}
end

procedure fword(n)
static fcache
initial fcache := table()
/fcache[n] := case n of {
1: "0"
2: "01"
default: fword(n-1)||fword(n-2)
}
return fcache[n]
end

procedure H(s)
P := table(0.0)
every P[!s] +:= 1.0/*s
every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
return h
end</lang>fw

Sample run:

<pre>
->fw
1 0.0 : 0
2 1.0 : 01
3 0.9182958340544: 010
4 0.9709505944546: 01001
5 0.9544340029249: 01001010
6 0.9612366047228: 0100101001001
7 0.9587118829771: 010010100100101001010
8 0.9596868937742
9 0.9593160320543
10 0.9594579158386
11 0.9594037542210
12 0.9594244469559
13 0.9594165437404
14 0.9594195626031
15 0.9594184095152
16 0.9594188499578
17 0.9594186817240
18 0.9594187459836
19 0.9594187214387
20 0.9594187308140
21 0.9594187272330
22 0.9594187286009
23 0.9594187280783
24 0.9594187282781
25 0.9594187282015
26 0.9594187282313
27 0.9594187282195
28 0.9594187282251
29 0.9594187282196
30 0.9594187282169
31 0.9594187282191
32 0.9594187282130
33 0.9594187282322
34 0.9594187281818
35 0.9594187282743
36 0.9594187282928
->
</pre>

Revision as of 16:41, 12 July 2013

Task
Fibonacci word
You are encouraged to solve this task according to the task description, using any language you may know.

The standard Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence:

Define F_Word 1 as 0;
Define F_Word 2 as 01;
Form F_Word 3 as F_Word 2 concatenated with F_Word 1 i.e "010"
Form F_Word n as F_Word n-1 concatenated with F_word n-2

For this task we shall do this for n = 32. You may display the first few but not the larger values of n, doing so will get me into trouble with them what be (again!).

Instead create a table which for F_Words 1 to 36 shows the Rep-string and Entropy.

Icon and Unicon

The following solution works in both Icon and Unicon. The first 7 Fibonacci words are shown, while the Entropy is shown for all 36. None of the fibonacci words have rep-strings, by the definition given in the Rep-string task.

<lang unicon>procedure main(A)

   n := integer(A[1]) | 36
   every w := fword(i := 1 to n) do {
       writes(right(i,4)," ",left(H(w),5))
       if i <= 7 then write(": ",w) else write()
       }

end

procedure fword(n)

   static fcache
   initial fcache := table()
   /fcache[n] := case n of {
                    1: "0"
                    2: "01"
                    default: fword(n-1)||fword(n-2)
                    }
   return fcache[n]

end

procedure H(s)

   P := table(0.0)
   every P[!s] +:= 1.0/*s
   every (h := 0.0) -:= P[c := key(P)] * log(P[c],2)
   return h

end</lang>fw

Sample run:

->fw
   1 0.0            : 0
   2 1.0            : 01
   3 0.9182958340544: 010
   4 0.9709505944546: 01001
   5 0.9544340029249: 01001010
   6 0.9612366047228: 0100101001001
   7 0.9587118829771: 010010100100101001010
   8 0.9596868937742
   9 0.9593160320543
  10 0.9594579158386
  11 0.9594037542210
  12 0.9594244469559
  13 0.9594165437404
  14 0.9594195626031
  15 0.9594184095152
  16 0.9594188499578
  17 0.9594186817240
  18 0.9594187459836
  19 0.9594187214387
  20 0.9594187308140
  21 0.9594187272330
  22 0.9594187286009
  23 0.9594187280783
  24 0.9594187282781
  25 0.9594187282015
  26 0.9594187282313
  27 0.9594187282195
  28 0.9594187282251
  29 0.9594187282196
  30 0.9594187282169
  31 0.9594187282191
  32 0.9594187282130
  33 0.9594187282322
  34 0.9594187281818
  35 0.9594187282743
  36 0.9594187282928
->