# Birthday Probability Problem

What is the probability that at least 2 people in a room of n people share the same birthday?

There are a huge amount of possibilities that might result in people sharing birthdays. Person A could share a birthday with person B, person C, or both! It’s even possible, though unlikely, that every person in the room share the exact same birthday! So how do we calculate the probability given the number of permutations that exist?

A common way to approach this problem using probability calculus is instead to calculate the probability of no-one sharing a birthday. As the probability of same and non-same birthdays are compliments, we can simply subtract this value from `1` to get the probability of same-birthdays.

Say we have `5` people in the room. We can calculate the total amount of permutations available with `365*364*363*362*361`. You may notice that this is almost factorial, except that we do not go all the way down to `1`. We can instead express this as `365! / (365 - n)!`. Note that computationally this is far far more expensive so you may wish to write your function using the former in a loop.

Once we have the total count of all possible permutations (k), we can get the probability by doing the following `k / 365^n`. This then gives us the probability of no-one sharing the same birthday.

With this value we can now figure out the probability of shared birthdays by subtracting this value from 1.

So to run through this in full, with `3` people.

• `365! / (365 - 3)!` equals `48228180`.
• `48228180 / 365^3` equals `0.991795834`.
• `1 - 0.991795834` equals `0.008204166`

There is a 0.8% probability with `3` people.

Some outputs of this are quite interesting:

There is 50% probability with `23` people. There is 99.99% probability with `70` people. Finally, 100% probability is of course reached with `>= 365` people.

Below is what this may look like in the Go language. Do note that we need to use the `math/big` package due to factorials over 20 exceeding the `uint64` size limit. Also mind this is very rough and probably has a tons of bug and areas for improvement!

``````package main

import "math"
import "math/big"

func bigFactorial(n int64) *big.Int {
if (n > 0) {
x := bigFactorial(n - 1)
m := big.NewInt(n)

return x.Mul(m, x)
}

return big.NewInt(1)
}

func birthdayProbability(num int64) float64 {
allPermutations := bigFactorial(365)
offsetPermutations := bigFactorial(365 - num)
totalPermutations := new(big.Int).Div(allPermutations, offsetPermutations)
totalBirthdays := big.NewFloat(math.Pow(365, float64(num)))

notSameProbability, _ := new(big.Float).Quo(new(big.Float).SetInt(totalPermutations), totalBirthdays).Float64()

return 1 - notSameProbability
}
``````