Skip to content

Instantly share code, notes, and snippets.

@addie
Last active December 17, 2015 14:59
Show Gist options
  • Save addie/8e29515b4f4b09d3f9f3 to your computer and use it in GitHub Desktop.
Save addie/8e29515b4f4b09d3f9f3 to your computer and use it in GitHub Desktop.
Fraction of the population that are girls if every couple keeps having children until they have a boy
# http://www.thebigquestions.com/2010/12/21/are-you-smarter-than-google/
# There’s a certain country where everybody wants to have a son.
# Therefore each couple keeps having children until they have a boy;
# then they stop. What fraction of the population is female? (30.7%)
# Probability Configuration
# 1/2 B
# 1/4 GB
# 1/8 GGB
# 1/16 GGGB
# Expected number of boys is
# (1/2)*1 + (1/4)*1 + (1/8)*1 + ... = 1
# Expected number of girls is
# (1/2)*0 + (1/4)*1 + (1/8)*2 + ... = 1
# Expected fraction of girls is
# (1/2)*0 + (1/4)*(1/2) + (1/8)*(2/3) + ... = 1-log(2)
topfrac=1.0
botfrac=2.0
probability = 1.0/2.0
i=0
sum=0
while (i < 100)
probability/=2
sum+=(topfrac/botfrac)*probability
topfrac+=1
botfrac+=1
i+=1
end
solution = 100-(Integer((1-sum)*1000)/10.0)
puts "The percentage of girls is approximately #{solution}%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment