Hello coders, today we are going to solve Day 4: Binomial Distribution HackerRank Solution which is a Part of 10 Days of Statistics Series.

Objective
In this challenge, we learn about binomial distributions.
Task
The ratio of boys to girls for babies born in Russia is 1.09 : 1. If there is 1 child born per birth, what proportion of Russian families with exactly 6 children will have at least 3 boys?
Write a program to compute the answer using the above parameters. Then print your result, rounded to a scale of 3 decimal places (i.e., 1.234 format).
Input Format
A single line containing the following values:
1.09 1
If you do not wish to read this information from stdin, you can hard-code it into your program.
Output Format
Print a single line denoting the answer, rounded to a scale of 3 decimal places (i.e., 1.234 format).
Solution – Day 4: Binomial Distribution I
C++
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; double b = 1.09, g = 1; // given int fact(int n) { if(n<2) return 1; return n*fact(n-1); } double nCr(int n, int r) { return fact(n)/(fact(r)*fact(n-r)); } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ double p = b/(b+g); double q = 1 - p; double prob = 0.0; for(auto i=6; i>=3; i--) { prob += nCr(6, i)*pow(p, i)*pow(q, 6-i); } printf("%0.3f\n", prob); return 0; }
Disclaimer: The above Problem (Binomial Distribution I) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.
def factorial(n):
result = 1
for i in range(2, n+1):
result *= i
return result
def bi_coef(n,k):
return factorial(n)/(factorial(k)*factorial(n-k))
p = 1.09/(1.09+1)
n = 6
pro = 0
for k in range(3, n+1):
pro += bi_coef(n, k) * (p ** k) * ((1 – p) ** (n – k))
print(f”{pro:.3f}”)