function[prob]=birthday(trialmax);

%Birthday.m will calculate the probability that 2 persons share the same
%birthday by running through trialmax trials for a given group size

%Loop through different amounts of persons
for a=2:70
    
    %for given amount of people, loop over addition of each person, once
    %match occurs, exit loop
    totalcount(a)=0;
    xvalues(a)=a;
    
    %run through trialmax trials for a persons
    for trials=1: trialmax

    %Introduce new people     
        for i=1:a
             match=0;
             birthday(i)=ceil(365*rand);    %Assign a random birthday to the ith person
             j=1;
             while j<i
                  %Does the new person have someone else's birthday?
                  if birthday(i)==birthday(j),match=1;,break,end;
                      j=j+1;
                  end
         if match==1,break,end;
        end
     if match==1;, count=1;,end;
     if match==0;, count=0;,end;
     totalcount(a)=totalcount(a)+count;
     end
prob(a)=totalcount(a)/trialmax;
end

%Determine when the probability is greater than 0.5
k=1;
while prob(k)<.5
    k=k+1;
end
disp(k);

%Deal with Plotting
plot(xvalues,prob,'*'); title('Probability that two people will share the same birthday');
xlabel('Number of People');ylabel('Fraction of times that people have birthday in common');
