function Birthday()
% Examines the liklihood that in a class of N students, at least three
% people have the same birth date. (Ignore leap year. But an interesting
% variation is to NOT ignore leap year!)
clc
fprintf(' N Probability Estimate\n------------------------------\n')
% Will base our probability predictions on this many experiments...
nEg = 1000;
for N = 20:5:100
% This variable will keep track of the number of times that we
% have 3 or more people swith the same birth date...
count = 0;
for j=1:nEg
% The vector of birth dates is a random vector of length N where
% each entry is randomly selected from the set {1,2,...,365}.
x = ceil(rand(1,N)*365);
if triple(x)
count = count+1;
end
end
prob = count/nEg;
fprintf(' %4d %6.3f\n',N,prob)
end
function alfa = triple(x)
x = sort(x);
n = length(x);
found = 0;
k = 1;
while k+2<=n && ~found
found = x(k)==x(k+1) && x(k+1)==x(k+2);
k=k+1;
end
alfa = found;