HackerRank Day 4: Binomial Distribution II | 10 Days of Statistics problem solution YASH PAL, 31 July 2024 In this Hackerrank Day 4: Binomial Distribution II 10 Days of Statistics problem A manufacturer of metal pistons finds that, on average, 12% of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of 10 pistons will contain: No more than 2 rejects? At least 2 rejects? 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) p, n = list(map(int, input().split(" "))) print(round(sum([b(i, n, p/100) for i in range(3)]), 3)) print(round(sum([b(i, n, p/100) for i in range(2, n+1)]), 3)) Problem solution in Java Programming. import java.io.*; import java.util.*; public class Solution { private static int fact(int n) { if(n <= 1) { return 1; } else { return n * fact(n-1); } } private static double bio(double a, double b, int end, int n) { double sum = 0; for(int i = 0;i <= end;i++) { int m = fact(n) / (fact(n-i) * fact(i)); sum += Math.pow(a, n-i) * Math.pow(b, i) * m; } return sum; } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scan = new Scanner(System.in); double percent = scan.nextInt() / 100.0; double pistons = scan.nextInt() / 100.0; System.out.println(String.format("%.3f", bio(1-percent, percent, 2, 10))); System.out.println(String.format("%.3f", bio(percent, 1-percent, 8, 10))); } } Problem solution in C++ programming. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; long long int gt(int a){ if (a<=1){ return 1; } else{ return a*gt(a-1); } } long long int th(int a,int b){ return gt(a)/(gt(b)*gt(a-b)); } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ double a,b; cin >> a >> b; double p=a/100; double r1,r2; r1=th(b,0)*pow(p,0)*pow(1-p,b-0)+th(b,1)*pow(p,1)*pow(1-p,b-1)+th(b,2)*pow(p,2)*pow(1-p,b-2); r2=1-(th(b,0)*pow(p,0)*pow(1-p,b-0)+th(b,1)*pow(p,1)*pow(1-p,b-1)); cout.precision(3); cout << r1 << endl; cout << r2; return 0; } Problem solution in C programming. #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> double binomial(int x, int n, double p) { double q = 1.0 - p; int comb = 1; int end = n - x; int i; for (i=n; i>end; i--) { comb = comb * i; } for (i=x; i>0; i--) { comb = comb / i; } // printf("x=%d, n=%d, comb=%d, p^x=%f, q^(n-x)=%f ", x, n, comb, pow(p,x), pow(q,n-x)); // printf("res=%fn", comb * pow(p,x) * pow(q,n-x)); return (comb * pow(p,x) * pow(q,n-x)); } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int reject_percent, batch_size; double p; // success probability double prejle2, prejge2; int i; scanf("%d %d", &reject_percent, &batch_size); p = 1 - (reject_percent/100.0); // printf("sucess prob p=%fn", p); // no more than 2 rejects for (i = 0, prejle2 = 0.0; i <= 2; i++) { prejle2 += binomial(batch_size - i, batch_size, p); } printf("%.3fn", prejle2); // at least 2 rejects for (i = 2, prejge2 = 0.0; i <= batch_size; i++) { prejge2 += binomial(batch_size - i, batch_size, p); } printf("%.3fn", prejge2); return 0; } Problem solution in JavaScript programming. function factorial (n) { var total = 1; for (i=1; i<=n; i++) { total = total * i; } return total; } function a_sobre_b(a,b) { return factorial(a)/(factorial(b) * factorial(a-b)); } function processData(input) { var suma_props = 0; for (var i=0; i<=2; i++) { suma_props += a_sobre_b(10,i)*Math.pow(0.12,i)*Math.pow(0.88,10-i); } console.log(suma_props.toFixed(3)); suma_props = 0; for (var i=0; i<=1; i++) { suma_props += a_sobre_b(10,i)*Math.pow(0.12,i)*Math.pow(0.88,10-i); } console.log((1-suma_props).toFixed(3)); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); 10 days of statistics coding problems