- #1

SherlockOhms

- 310

- 0

## Homework Statement

So, you're asked to write a function that computes the probability of two people at a party having the same birthday. The probability function itself is

P(n) = 0, n = 1

P(n) = 1 - (capital pi from k = 1 to k = n - 1)(1 - k/365), 2 <=n <= 365

P(n) = 1, n >= 366

The function is used to calculate the minimum value of m (your chosen n) for which P(m) >= q. Where q (0, 1], i.e. an arbitrary probability entered by the user. The function should accept q as an input and return m as an output.

## Homework Equations

## The Attempt at a Solution

Code:

```
function [m] = Prob(q)
m = 0;
P = 0;
while P < q
m = m + 1;
if m == 1
P = 0;
elseif m >= 2 && m <= 365
S = 1;
for k = 1: m - 1
S = S * (1 - k/365);
end
P = 1 - S;
else
P = 1;
end
end
```

Essentially, I want the program to accept a value of q and the to run until the value of P is greater than or equal to q. Once the the value of P is returned, m will have it's minimum value for which P >= q due to m being incremented during each iteration of the while loop. However, for some reason the value of "m"s being returned is too low and I can't seem to see why that is. For example, when I enter a value of 1 into the function, m is returned as 153 an not 366. Can anyone spot why this may be? I've looked through the code a few times now and can't seem to spot why. Is the incrementation being skipped or is there a problem with the nested for loop which calculates the value of P that involves the product of 1 - k/365 from k = 1, to k = m - 1? Thanks for any possible comments in advance. (Sorry, had to use underscores for indenting)

Last edited by a moderator: