In this Hackerrank Day 6: The Central Limit Theorem I 10 Days of Statistics problem A large elevator can transport a maximum of 9800 pounds. Suppose a load of cargo containing 49 boxes must be transported via the elevator. The box weight of this type of cargo follows a distribution with a mean of 205 pounds and a standard deviation of 15 pounds. Based on this information, what is the probability that all 49 boxes can be safely loaded into the freight elevator and transported?

HackerRank Day 6: The Central Limit Theorem I | 10 Days of Statistics solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import math

x = int(input())
n = int(input())
mu = int(input())
sigma = int(input())

mu_sum = n * mu 
sigma_sum = math.sqrt(n) * sigma

def cdf(x, mu, sigma):
    Z = (x - mu)/sigma
    return 0.5*(1 + math.erf(Z/(math.sqrt(2))))

print(round(cdf(x, mu_sum, sigma_sum), 4))



Problem solution in Java Programming.

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

public class Solution {
    public static double erf(double z, double x)
    {
        int direction = z>0?1:-1;
        double result =0;
        double delta = .000001;
        double iz = 0;
        while(iz*direction<z*direction) {
            result+=delta*Math.pow(Math.E,-(iz*iz))*direction;
            iz+=delta*direction;
        }
        //System.out.printf("result=%.5f\n",result);
        return 2./Math.sqrt(Math.PI) * result;
    }
    public static double normal_dist_func(double x, double mu, double stddev)
    {
        double z = ((x-mu)/(stddev*Math.sqrt(2)));
        //System.out.println("z=" +z + ", " + "erf=" + erf(z,x));
        return .5*(1+erf(z,x));
    }
    
    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 mean = 205*49;
        double stddev = 15*Math.sqrt(49);
        System.out.printf("%.4f\n",normal_dist_func(9800,mean,stddev));
    }
}


Problem solution in C++ programming.

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

double phi(double x){
    return (exp(-x*x*0.5)/sqrt(2*M_PI));
}

double normal(double mean, double sigma, double x){
    return phi((x-mean)/sigma)/sigma;
}

double erf(double x){
    int n = 100;
    double dt = x / n;
    double sum = 0;
    for (int i = 0; i < n; i++){
        double z = i * dt + 0.5 * dt;
        sum += exp(-z * z) * dt;
    }
    return sum * 2 / sqrt(M_PI);
}

double Phi(double mean, double sigma, double x){
    return 0.5 * (1 + erf((x - mean)/(sigma * sqrt(2))));
}


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    double mean = 205;
    double sigma = 15;
    int n = 49;
    double mean2 = n * mean;
    double sigma2 = sqrt(n) * sigma;
    double x = 9800;
    printf("%.4f", Phi(mean2, sigma2, x));
    return 0;
}


Problem solution in C programming.

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

int main() {

  double weight,count,meanv,stdv;

  scanf("%lf",&weight);
  scanf("%lf",&count);
  scanf("%lf",&meanv);
  scanf("%lf",&stdv);
  

  printf("%6.4lf\n",0.5*(1.0+erf((weight-(count*meanv))/(sqrt(count)*stdv*sqrt((double)2.0)))));
  
  return 0;
}


Problem solution in JavaScript programming.

const print = o => console.log(o.toFixed(4));

function processData(input) {
    const [maxWeight, numBoxes, meanWeightBox, sdWeightBox] = input.split`\n`.map(Number);
    
    console.error(maxWeight, numBoxes, meanWeightBox, sdWeightBox);
        
    const z = ((maxWeight/numBoxes) - meanWeightBox) / (sdWeightBox / Math.sqrt(numBoxes))
    print(0.5 - 0.49010)
    
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

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