function p = marks ( n )

%*****************************************************************************80
%
%% marks() analyzes the marks problem.
%
%  Discussion:
%
%    Suppose we break each of N sticks randomly into 2 parts, a long part 
%    and a short part.  Then we create N new sticks by randomly selecting
%    (without replacement) pairs of parts.
%
%    What is the probability that every new stick consists of a long part
%    and a short part?
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    07 May 2019
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Input:
%
%    integer N, the number of sticks.
%
%  Output:
%
%    real P: the estimated probability.  The exact value is 2/(N+1).
%
  trial_num = 100000;

  same = 0;
  for trials = 1 : trial_num
    x = rand ( n - 1, 1 );
    cut = sort ( x );
    cut(n) = 1;
    y = rand ( 2, 1 );
    look = 1;
    k = 1;

    while ( look == 1 )
      if ( cut(k) < y(1) )
        k = k + 1;
      else
        remember = k;
        look = 0;
      end
    end

    if ( remember == 1 ) 
      if ( y(2) < cut(remember) )
        same = same + 1;
      end
    else
      if ( y(2) < cut(remember) && cut(remember-1) < y(2) )
        same = same + 1;
      end
    end

  end

  p = same / trial_num;

  return
end
