Related Articles

# Puzzle | The Apocalypse

• Last Updated : 19 Jul, 2021

Puzzle:
In the new post-apocalyptic world, the world queen is desperately concerned about the birth rate. Therefore, she decrees that all families should ensure that they have one girl or else they face massive fines. If all families abide by this policy-that is, they have continued to have children until they have one girl, at which point they immediately stop-what will the gender ratio of the new generation be? (Assume that the odds of someone having a boy or a girl on any given pregnancy is equal.) Solve this out logically and then write a computer simulation of it.

Hints:

1. Observe that each family will have exactly one girl.
2. Think about writing each family as a sequence of Bs and Gs.

Solution:

• If each family abides by this policy, then each family will have a sequence of zero or more boys followed by a single girl.
• That is if “G” indicates a girl and “B” indicates a boy, the sequence of children will look like one of G; BG; BBG; BBBG; BBBBG; and so on.

Mathematically:

• It can work out the probability for each gender sequence.
1. P(G) = 1/2-
That is, 50% of families will have a girl first. The others will go on to have more children.
2. P(BG) = 1/4-
Of those who have a second child (which is 50%), 50% of them will have a girl the next time.
3. P(BBG) = 1/8-
Of those who have a third child (which is 25%), 50% of them will have a girl the next time.  And so on.
• It is given that every family has exactly one girl.
• How many boys does each family have, on average? To compute this, let’s look at the expected value of the number of boys.
• The expected value of the number of boys is the probability of each sequence multiplied by the number of boys in that sequence.

Below is the table illustrating the expected value of number of boys:

• Or in other words, this is the sum of i to infinity of i divided by 2i.

Logically:

• One way to think about this is to imagine that we put all the gender sequences of each family into one giant string. So if family 1 has BG, family 2 has BBG, and family 3 has G, it would be BGBBGG.
• As soon as a child is born, its gender(B or G) can be appended to the string.
• What are the odds of the next character being a G? Well, if the odds of having a boy and girl is the same, then the odds of the next character being a G is 50%.
• Therefore, roughly half of the string should be Gs and half should be Bs, giving an even gender ratio.
• This actually makes a lot of sense. Biology hasn’t been changed.
• Half of the newborn babies are girls and half are boys.
• Abiding by some rules about when to stop having children doesn’t change this fact.
• Therefore, the gender ratio is 50% girls and 50% boys.

Below is the pseudo-code calculating the probability of having a girl in the family-

```double r unNFamilies (int n)
{
int boys = 0;
int girls = 0;
for (int i = 0; i < n; i++)
{
int [] genders = runOneFamilY();
girls += genders[0];
boys += genders[1];
}
return girls / (double) (boys + girls);
}

int[] runOneFamily()
{
Random random = new Random();
int boys = 0;
int girls = 0;
while (girls == 6)
{
// until we have a girl
if (random.nextBoolean ())
{
// girl
girls += 1;
}
else
{
// boy
boys += 1;
}
}
int[] genders = {girls, boys};
return genders;
}```

If this psuedo-code is executed on large values if n, then the result will be very close to 0.5.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up