function stopping ( n )

%*****************************************************************************80
%
%% STOPPING analyzes an optimal stopping problem.
%
%  Discussion:
%
%    From a population of N indexed values, the highest is sought.
%    Values are to be discovered in order of index.
%    When value I is discovered:
%    * it may be rejected, and the next value discovered, or
%    * it may be accepted, and the process is terminated.
%    A strategy is to view S items in a row, and then accept 
%    the very next item that is larger than max(S).
%    Given N, what is S, and what are the chances that this process will
%    produce the maximum?
%    
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Input parameters:
%
%    Input, integer N, the number of choices.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'STOPPING:\n' );
  fprintf ( 1, '  From a population of N indexed values, the highest is sought.\n' );
  fprintf ( 1, '  Values are to be discovered in order of index.\n' );
  fprintf ( 1, '  When value I is discovered:\n' );
  fprintf ( 1, '  * it may be rejected, and the next value discovered, or\n' );
  fprintf ( 1, '  * it may be accepted, and the process is terminated.\n' );
  fprintf ( 1, '  A strategy is to view S items in a row, and then accept \n' );
  fprintf ( 1, '  the very next item that is larger than max(S).\n' );
  fprintf ( 1, '  Given N, what is S, and what are the chances that this process will\n' );
  fprintf ( 1, '  produce the maximum?\n' );

  prob = zeros ( 1, n );
  for r = 2 : n
    s = 0;
    for j = r : n
      s = s + 1 / ( j - 1 );
    end
    prob(r) = ( r - 1 ) * s / n;
  end

  [ maximum, index ] = max ( prob );
  best = n / ( index - 1 );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Population size N      = %d\n', n );
  fprintf ( 1, '  Optimal sample size S  = %d\n', index - 1 );
  fprintf ( 1, '  Ratio N / S            = %g\n', n / ( index - 1 ) );
  fprintf ( 1, '  Probability of success = %g\n', prob(index) );

  return
end
