Hi there! Today’s challenge from HackerRank was a little more involved. The
challenge is *Given the ratio* $1.09 : 1$ *of boys to girls for babies born in
a certain country, what is the proportion of families with exactly 6 children
that will have at least 3 boys?*. Before actually solving that, it is important
to understand a Bernoulli trial and also the Binomial Distribution.

### Bernoulli trial

According to Wikipedia, a Bernoulli trial is a random experiment with exactly two possible outcomes, “success” and “failure”, in which the probability of success is the same every time the experiment is conducted.

Regarding this problem, each random experiment is having a baby and the probability of “success” is the probability of having a baby boy that is $p = \frac{1.09}{1.09 + 1}$. If it asked for girls instead of boys in the end, success would be having a baby girl and this probability would be $p = \frac{1}{1.09 + 1}$.

### Binomial Distribution

According to Wikipedia, the binominal distribution with parameters $n$ and $p$ is the discrete probability distribution of the number of successes $x$ in a sequence of $n$ independent Bernoulli trials, each having a probability of success $p$. It is given by the formula $$b(x,n,p) = \left(\begin{array}{c}n\ x\end{array}\right) p^x (1-p)^{(n-x)}.$$

For the case in which we want the probability of having at least three boys for a family with 6 children, we sum the values of the binomial distribution: $$\mathrm{Pr} = \sum_{i=3}^{6} b(i,6,p).$$

### Code

My solution is given below.

```
package main
import (
"fmt"
"math"
)
func factorial(n int) int {
if n <= 1 {
return 1
} else {
return n * factorial(n-1)
}
}
func b(x, n int, p float64) float64 {
comb_n_x := float64(factorial(n) /
factorial(x) /
factorial(n-x))
p_x := math.Pow(p, float64(x))
q_n_x := math.Pow(1.0-p, float64(n-x))
return comb_n_x * p_x * q_n_x
}
func main() {
// reading the proportion
// of boys to girls
boys := 0.0
fmt.Scanf("%f", &boys)
girls := 0.0
fmt.Scanf("%f", &girls)
// probability of having a boy
p := boys / (girls + boys)
// total number of trials
n := 6
// probability of having
// at least 3 boys
probability := 0.0
for i := 3; i <= n; i++ {
probability += b(i, n, p)
}
fmt.Printf("%.3f\n", probability)
}
```

That’s all for today! Thanks for reading!