Task: Fibonacci word


C++: #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 ;
}










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;
use  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;

procedure Fibonacci_Words is

   function Entropy (S : Unbounded_String) return Long_Float is
      CF    : array (Character) of Natural := (others => 0);
      Len   : constant Natural             := Length (S);
      H     : Long_Float                   := 0.0;
      Ratio : Long_Float;
   begin
      for I in 1 .. Len loop
         CF (Element (S, I)) := CF (Element (S, I)) + 1;
      end loop;
      for C in Character loop
         Ratio := Long_Float (CF (C)) / Long_Float (Len);
         if Ratio /= 0.0 then
            H := H - Ratio * Log (Ratio, 2.0);
         end if;
      end loop;
      return H;
   end Entropy;

   procedure Print_Line (Word : Unbounded_String; Number : Integer) is
   begin
      Put (Number, 4);
      Put (Length (Word), 10);
      Put (Entropy (Word), 2, 15, 0);
      if Length (Word) < 35 then
         Put ("  " & Word);
      end if;
      New_Line;
   end Print_Line;

   First, Second, Result : Unbounded_String;

begin
   Set_Col (4);  Put ("N");
   Set_Col (9);  Put ("Length");
   Set_Col (16); Put ("Entropy");
   Set_Col (35); Put_Line ("Word");
   First := To_Unbounded_String ("1");
   Print_Line (First, 1);
   Second := To_Unbounded_String ("0");
   Print_Line (Second, 2);
   for N in 3 .. 37 loop
      Result := Second & First;
      Print_Line (Result, N);
      First  := Second;
      Second := Result;
   end loop;
end Fibonacci_Words;