Skip to content

Instantly share code, notes, and snippets.

@ramiroaisen
Created March 8, 2024 20:05
Show Gist options
  • Save ramiroaisen/8a20825510d7278e9320ee296357dfb2 to your computer and use it in GitHub Desktop.
Save ramiroaisen/8a20825510d7278e9320ee296357dfb2 to your computer and use it in GitHub Desktop.
Birthday problem collision calculator in javascript
/**
* @param p {number} - The total number of different possibilities (days in a year)
* @param n {number} - The number of occurrences (number of people)
* @returns {number} - The probability of at least two people having the same birthday. @min 0 - @max 1
*/
export const birthday_problem = (p, n) => {
let acc = 1;
for(let i = 1; i < n; i++) {
acc *= (1 - ( i / p ));
}
return 1 - acc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment