I am trying to solve a version of the birthday paradox question where I have a probability of 0.5 but I need to find the number of people n where at least 4 have their birthdays within a week of each other.

I have written code that is able to simulate where 2 people have their birthdays on the same day.

```import numpy
import matplotlib.pylab as plt

no_of_simulations = 1000

milestone_probabilities = [50, 75, 90, 99]
milestone_current = 0

global milestone_probabilities, milestone_current

same_birthday_four_people = 0

#We assume that there are 365 days in all years.
for sim in range(simulations):
birthdays = numpy.random.choice(365, no_of_people, replace=True)
unique_birthdays = set(birthdays)
if len(unique_birthdays) < no_of_people:
same_birthday_four_people += 1

success_fraction = same_birthday_four_people/simulations

if milestone_current < len(milestone_probabilities) and success_fraction*100 > milestone_probabilities[milestone_current]:
print("P(Four people sharing birthday in a room with " + str(no_of_people) + " people) = " + str(success_fraction))
milestone_current += 1

return success_fraction

def main():
day = []
success = []
for i in range(1, 366):     #Executing for all possible cases where can have unique birthdays, i.e. from 1 person to a maximum of 365 people in a room
day.append(i)

plt.plot(day, success)
plt.show()

main()
```

I am looking to modify the code to look for sets of 4 instead of 2 and then calculate the difference between them to be less than equal to 7 in order to meet the question.

Am I going down the right path or should I approach the question differently?

The key part of your algorithm is in these lines:

```unique_birthdays = set(birthdays)
if len(unique_birthdays) < no_of_people:
same_birthday_four_people += 1
```

Comparing the number of unique birthdays to the number of people did the work when you tested if two different people had the same birthday, but It wont do for your new test.

Define a new function that will receive the birthday array and return `True` or `False` after checking if indeed 4 different people had the a birthday in a range of 7 days:

```def four_birthdays_same_week(birthdays):
# fill this function code

...
```

(this function can be defined outside the `birthday_paradox` function)

Then switch this code:

```if len(unique_birthdays) < no_of_people:
same_birthday_four_people += 1
```

into:

```if four_birthdays_same_week(birthdays):
same_birthday_four_people += 1
```

Regarding the algorithm for checking if there 4 different birthday on the same week: a basic idea would be to sort the array of birthdays, then for every group of 4 birthdays check if the day range between them is equal or lower to 7:
if it is, the function can immediately return `True`. (I am sure this algorithm can be vastly improved.)

If after scanning the whole array we didn’t return `True`, the function can return `False`.