Fibonacci word: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 722: | Line 722: | ||
36 14930352 0.9594187282227 |
36 14930352 0.9594187282227 |
||
37 24157817 0.9594187282227 |
37 24157817 0.9594187282227 |
||
</pre> |
|||
=={{header|C sharp}}== |
|||
<lang C sharp> |
|||
using SYS = System; |
|||
using SCG = System.Collections.Generic; |
|||
// |
|||
// Basically a port of the C++ solution as posted |
|||
// 2017-11-12. |
|||
// |
|||
namespace FibonacciWord |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main( string[] args ) |
|||
{ |
|||
PrintHeading(); |
|||
string firstString = "1"; |
|||
int n = 1; |
|||
PrintLine( n, firstString ); |
|||
string secondString = "0"; |
|||
++n; |
|||
PrintLine( n, secondString ); |
|||
while ( n < 37 ) |
|||
{ |
|||
string resultString = firstString + secondString; |
|||
firstString = secondString; |
|||
secondString = resultString; |
|||
++n; |
|||
PrintLine( n, resultString ); |
|||
} |
|||
} |
|||
private static void PrintLine( int n, string result ) |
|||
{ |
|||
SYS.Console.Write( "{0,-5}", n ); |
|||
SYS.Console.Write( "{0,12}", result.Length ); |
|||
SYS.Console.WriteLine( " {0,-16}", GetEntropy( result ) ); |
|||
} |
|||
private static double GetEntropy( string result ) |
|||
{ |
|||
SCG.Dictionary<char, int> frequencies = new SCG.Dictionary<char, int>(); |
|||
foreach ( char c in result ) |
|||
{ |
|||
if ( frequencies.ContainsKey( c ) ) |
|||
{ |
|||
++frequencies[c]; |
|||
} |
|||
else |
|||
{ |
|||
frequencies[c] = 1; |
|||
} |
|||
} |
|||
int length = result.Length; |
|||
double entropy = 0; |
|||
foreach ( var keyValue in frequencies ) |
|||
{ |
|||
double freq = (double)keyValue.Value / length; |
|||
entropy += freq * SYS.Math.Log( freq, 2 ); |
|||
} |
|||
return -entropy; |
|||
} |
|||
private static void PrintHeading() |
|||
{ |
|||
SYS.Console.Write( "{0,-5}", "N" ); |
|||
SYS.Console.Write( "{0,12}", "Length" ); |
|||
SYS.Console.WriteLine( " {0,-16}", "Entropy" ); |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
{{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.959418721438676 |
|||
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> |
</pre> |
||