Header Ad

HackerRank Day 4: Geometric Distribution II | 10 Days of Statistics problem solution

In this Hackerrank Day 4: Geometric Distribution II 10 Days of Statistics problem The probability that a machine produces a defective product is 1/3. What is the probability that the 1st defect is found during the first 5 inspections?

HackerRank Day 4: Geometric Distribution II | 10 Days of Statistics problem solution


Problem solution in Python programming.

p=1/3

def geomprob(k,p):
    return p*(1-p)**(k-1)

print("{0:.3f}".format(sum([geomprob(k,p) for k in range(1,6)])))



Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        double p=1.0/3;
        double q=1.0-p;
        double sum=0.0;
        for(int n=1; n<=5; n++)
            {
                double val = 0.0;
                val = Math.pow(q,(n-1))*p;
                sum += val;
        }
    
         DecimalFormat newFormat = new DecimalFormat("#.###");
         System.out.println(newFormat.format(sum));
        
    }
}


Problem solution in C++ programming.

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

float g( int n, float p)
{
    return pow((1-p),float(n-1))*p;    
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    float a;
    float b;
    int n;
    cin >> a;
    cin >> b;
    cin >> n;
    float p = a/b;
    cout.precision(3);
    float gv;
    for( int i = n; i >0;i--)
        gv+=g(i,p);
    cout << fixed << gv;
    return 0;
}


Problem solution in C programming.

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

double geometric(int n, double p) {
    double q = 1 - p; 
    return (pow(p,n-1) * q); 
}

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int fail_num, fail_den;
    int i, n;
    double p;
    double p_first5 = 0;
    
    scanf("%d %d\n", &fail_num, &fail_den);
    scanf("%d", &n);
    p = 1 - (((double)fail_num)/fail_den);
    
    for (i=1; i<=5; i++) {
        p_first5 += geometric(i, p);
    }
    printf("%.3f", p_first5);
    
    return 0;
}


Problem solution in JavaScript programming.

function choose(n, k) {
    if (k === 0) return 1;
    return (n * choose(n-1, k-1)) / k;
}

const geometricDist = function(n, p, q) {
    return Math.pow(q, n - 1) * p;
}

const negBinomialDist = function(x, n, p, q) {
    return choose(n - 1, x - 1) * Math.pow(p, x) * Math.pow(q, n - x);
}

function processData(input) {
    const p = 1 / 3;
    const q = 1 - p;
    const n = 5;
    const x = 1;
    
    var ans = 0; 
    
    for(var i = 1; i <= 5; i++) {
        ans += geometricDist(i, p, q);
    }
    
    console.log(ans.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);
});


Post a Comment

0 Comments