Skip to content

Instantly share code, notes, and snippets.

@hazycora
Last active January 2, 2025 13:20
Show Gist options
  • Save hazycora/7c2f2f20320bdf4100e2712827ac0e86 to your computer and use it in GitHub Desktop.
Save hazycora/7c2f2f20320bdf4100e2712827ac0e86 to your computer and use it in GitHub Desktop.
// A solution to the following problem:
// "I have two children, (at least) one of whom is a boy born on a Tuesday-
// what is the probability that both children are boys?"
// Problem proposed here: https://x.com/pli_cachete/status/1873822656879091908
const sexes = ['male', 'female']
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const possibleChildren = sexes.map(sex => days.map(day => [sex, day])).flat()
const possibleChildPairs = possibleChildren.map(child => possibleChildren.map(otherChild => [child, otherChild])).flat()
// possibleChildPairs is now a list of every combination of either male/female with each day of the week.
// both males born on tuesdays is already in possibleChildPairs, but we have to count it *twice*.
// we do not know which child is the boy born on a tuesday, the first or the second?
// to count either case, this should be in the list *two times*.
possibleChildPairs.push([['male', 'Tuesday'], ['male', 'Tuesday']])
const atLeastOneTuesdayMale = possibleChildPairs.filter(([childOne, childTwo]) => {
if (childOne[0] == 'male' && childOne[1] == 'Tuesday') {
return true
}
if (childTwo[0] == 'male' && childTwo[1] == 'Tuesday') {
return true
}
return false
})
const bothBoys = atLeastOneTuesdayMale.filter(([childOne, childTwo]) => {
return childOne[0] == 'male' && childTwo[0] == 'male'
})
console.log(bothBoys.length / atLeastOneTuesdayMale.length)
// 0.5, or 50%
// A (incorrect) solution to the following problem:
// "I have two children, (at least) one of whom is a boy born on a Tuesday-
// what is the probability that both children are boys?"
// Problem proposed here: https://x.com/pli_cachete/status/1873822656879091908
const sexes = ['male', 'female']
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const possibleChildren = sexes.map(sex => days.map(day => [sex, day])).flat()
const possibleChildPairs = possibleChildren.map(child => possibleChildren.map(otherChild => [child, otherChild])).flat()
// possibleChildPairs is now a list of every combination of either male/female with each day of the week.
const atLeastOneTuesdayMale = possibleChildPairs.filter(([childOne, childTwo]) => {
if (childOne[0] == 'male' && childOne[1] == 'Tuesday') {
return true
}
if (childTwo[0] == 'male' && childTwo[1] == 'Tuesday') {
return true
}
return false
})
const bothBoys = atLeastOneTuesdayMale.filter(([childOne, childTwo]) => {
return childOne[0] == 'male' && childTwo[0] == 'male'
})
console.log(bothBoys.length / atLeastOneTuesdayMale.length)
// 0.48148148148148145, or ~48%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment