Skip to content

Instantly share code, notes, and snippets.

@quinnlas
Last active April 3, 2023 03:48
Show Gist options
  • Save quinnlas/8074e88a73967ea8e16f5b2f11879031 to your computer and use it in GitHub Desktop.
Save quinnlas/8074e88a73967ea8e16f5b2f11879031 to your computer and use it in GitHub Desktop.
birthday problem calculator
let birthday = n => {let r=1;for(let i=1;i<n;i++){r*=(366-i)/366}return 1-r}
@quinnlas
Copy link
Author

quinnlas commented Apr 3, 2023

Explanation:

// gives the chance of two people having the same birthday, given the number of people
function birthday(numPeople) {
  // the probability of have no match for the first person is 1, since there is no one to match to
  let failChance = 1;
  for (let checkedPpl = 1; checkedPpl < numPeople; checkedPpl++) {
    const remainingBirthdays = 366 - checkedPpl
    const chanceOfNewBirthday = remainingBirthdays / 366
    failChance *= chanceOfNewBirthday
  }
  return 1 - failChance
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment