Created
March 8, 2024 20:05
-
-
Save ramiroaisen/8a20825510d7278e9320ee296357dfb2 to your computer and use it in GitHub Desktop.
Birthday problem collision calculator in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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