function esim ( n )

%*****************************************************************************80
%
%% esim() estimates E by a binning process.
%
%  Discussion:
%
%    Sample N points in the unit interval.
%    Put them into N bins of equal width.
%    Let Z be the number of empty bins.
%    N/Z is an estimate of E. 
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer n: the number of sample points.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'esim():\n' );
  fprintf ( 1, '  Sample n=%d points in the unit interval.\n', n );
  fprintf ( 1, '  Put them into n bins of equal width.\n', n );
  fprintf ( 1, '  Let Z be the number of empty bins.\n' );
  fprintf ( 1, '  n/Z is an estimate of E. \n' );

  bin = zeros(1,n);
  for j = 1 : n
    number = floor ( n * rand ( ) ) + 1;
    bin(number) = bin(number) + 1;
  end

  empty = 0;
  for j = 1 : n
    if ( bin(j) == 0 )
      empty = empty + 1;
    end
  end

  e_estimate = n / empty;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimate for E = %g\n', e_estimate );
  fprintf ( 1, '  Exact value =    %g\n', exp ( 1.0 ) );

  return
end

