# HackerRank Day 4: Binomial Distribution I | 10 Days of Statistics solution

In this Hackerrank Day 4: Binomial Distribution I 10 Days of Statistics problem we have Given The ratio of boys to girls for babies born in Russia is 1.09. 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.

## Problem solution in Python programming.

```# Enter your code here. Read input from STDIN. Print output to STDOUT
def fact(n):
return 1 if n == 0 else n*fact(n-1)

def comb(n, x):
return fact(n) / (fact(x) * fact(n-x))

def b(x, n, p):
return comb(n, x) * p**x * (1-p)**(n-x)

l, r = list(map(float, input().split(" ")))
odds = l / r
print(round(sum([b(i, 6, odds / (1 + odds)) for i in range(3, 7)]), 3))```

## Problem solution in Java Programming.

```import java.util.Scanner;

public class BinomialDistributionI {

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
double r = kb.nextDouble();
double c = kb.nextDouble();
double b = r/(r+c);
int x = 6;
double prob = 0;
for(int i=3;i<=x;i++)
prob+=Math.pow(b,i)*Math.pow(1-b,x-i)/(f(i)*f(x-i));
System.out.printf("%.3f",f(x)*prob);
kb.close();
}
public static int f(int n){
return n==0 ? 1 : n*f(n-1);
}
}```

## Problem solution in C++ programming.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

long factorials [100];

long factorial(int n) {
if (factorials[n] != 0) return factorials[n];
else factorials[n] = (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
return factorials[n];
}

double binomialDistribution(int x, int n, double p) {
int binomialCoefficient;
binomialCoefficient = factorial(n) / (factorial(x) * factorial(n - x));

return binomialCoefficient * pow(p, x) * pow(1 - p, n - x);
}

double russianFamily(double boys, double girls, int numChildren, int minBoys) {
double p = boys / (boys + girls);

double cumulativeProbability = 0;

for (int x = minBoys; x <= numChildren; x++) {
cumulativeProbability += binomialDistribution(x, numChildren, p);
}

return cumulativeProbability;
}

int main() {
/*double boys;
double girls;
cin >> boys >> girls;*/

double proportion = russianFamily(1.09, 1, 6, 3);
cout << fixed << setprecision(3) << proportion;

return 0;
}```

## Problem solution in C programming.

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int C(int n, int x){
if(x == n) return 1;
if(1 == x) return n;
return C(n - 1, x) + C(n - 1, x - 1);
}

double binominal(int n, int x, double p, double q){
return C(n, x) * pow(p, x) * pow(q, 6 - x);
}

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
double p = 1.09/(1.09 + 1);
double q = 1 - p;
double ans = 0;
for(int i = 3; i <= 6; ++i){
ans += binominal(6, i, p, q);
}
printf("%.3f", ans);
return 0;
}```

## Problem solution in JavaScript programming.

```function processData(input) {
var p = parseFloat(input.split(" ")[0])/(parseFloat(input.split(" ")[0]) + parseFloat(input.split(" ")[1]));
var q = 1-p;
var n = 6;
var x = 3;

var sum = 0;
for(let i = x; i<=n; i++){
sum+=binDist(i,n,p,q);
}
console.log(sum.toFixed(3));
}

function binDist(x,n,p,q){
return combPerm(n, x) * Math.pow(p, x) * Math.pow(q, (n-x));
}
function combPerm(n, x){
return factorial(n)/(factorial(x)*factorial(n-x));
}
function factorial(num)
{
// If the number is less than 0, reject it.
if (num < 0) {
return -1;
}
// If the number is 0, its factorial is 1.
else if (num == 0) {
return 1;
}
// Otherwise, call this recursive procedure again.
else {
return (num * factorial(num - 1));
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```