% [pick_num.m] 
% For different values of k, k people pick a number from $\{1, \dots  n\}$.
% Find the LARGEST value of n so that:
% the probability  that {\bf  AT LEAST 2} pick the SAME number
% is greater than a given probabiity of success=p1 e.g. p1= .95 or p1= .99 
% Note that the program works with  q = Prob( all DIFFERENT numbers)
% so that q <= (1 - p1) 
% 

p1= input('what is the desired level p1 of success, e.g. p1 =.95? ');

% enter range as vector where the upper value equals the number
% of people picking a number
%  
range = input('what is the range.  [start: interval: number people], e.g [5:5:100]?  ');

store = []; % start with an empty storage vector

for k = range;
  
   n = k;  % for n < k-1, prob at least 2 pick same  = 1 (pigeon hole principle)
   q = 0;  % when n = k the probability is virtually 0
    
      while q < 1 - p1
          n = n + 1;
          a = [n: -1 : (n-k+1)];
          b = (1/n)*(a);
         q  = prod(b);
     endwhile

   store = [store; k, (n-1)];    % concatenate new values

endfor

disp('desired level p1  = '), disp(p1)
disp(store)

