In this Hackerrank Day 6: The Central Limit Theorem II 10 Days of Statistics problem The number of tickets purchased by each student for the University X vs. University Y football game follows a distribution that has a mean of 2.4 and a standard deviation of 2.0.

A few hours before the game starts, 100 eager students line up to purchase last-minute tickets. If there are only 250 tickets left, what is the probability that all 100 students will be able to purchase tickets?

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


Problem solution in Python programming.

import math

numTic = float(input())
numStd = float(input())
mu = numStd * float(input())
sig = math.sqrt(numStd) * float(input())

print(round(0.5*(1+math.erf((numTic - mu)/(sig * math.sqrt(2)))),4))



Problem solution in Java Programming.

import java.util.Scanner;
import java.util.stream.IntStream;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int maxTicketNum = sc.nextInt();
		int n = sc.nextInt();
		double mean = sc.nextDouble();
		double std = sc.nextDouble();

		System.out.printf("%.4f\n", solve(maxTicketNum, n, mean, std));

		sc.close();
	}

	static double solve(int maxTicketNum, int n, double mean, double std) {
		return phi(n * mean, Math.sqrt(n) * std, maxTicketNum);
	}

	static double phi(double mean, double std, double x) {
		return 0.5 * (1 + erf((x - mean) / (std * Math.sqrt(2))));
	}

	// https://en.wikipedia.org/wiki/Error_function#Taylor_series
	static double erf(double z) {
		double result = 0;
		for (int n = 0;; n++) {
			double term = 2 / Math.sqrt(Math.PI) * ((n % 2 == 0) ? 1 : -1) * Math.pow(z, 2 * n + 1)
					/ (factorial(n) * (2 * n + 1));

			if (Math.abs(term) < 1e-5) {
				break;
			}

			result += term;
		}
		return result;
	}

	static double factorial(int n) {
		return IntStream.rangeClosed(1, n).mapToDouble(x -> x).reduce(1, (x, y) -> x * y);
	}
}


Problem solution in C++ programming.

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


int main() {

    int max, N;
    float mu, sigma;

    cin >> max >> N >> mu >> sigma;

    float central_mu = 1.0f * mu * N;
    float central_sigma = 1.0f * sigma * pow(N,0.5);

    float ans = ( erf( (max-central_mu)/central_sigma/pow(2,0.5) ) + 1  )*0.5;

    cout << ans;


    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}


Problem solution in C programming.

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

int main() {
    int n = 100;
    double mean = n * 2.4;
    double sd = 2.0 * pow(n, 0.5);

    double res = erf((250 - mean) / (sd * sqrt(2.0)));

    printf("%.4f", (0.5) * (1 + res));

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}


Problem solution in JavaScript programming.

function erf(x) {
    //jacked from the internet
    let a1 = 0.254829592,
        a2 = -0.284496736,
        a3 = 1.421413741,
        a4 = -1.453152027,
        a5 = 1.061405429,
        p = 0.3275911

    let sign = 1

    if (x < 0)
        sign = -1

    x = Math.abs(x)

    let t = 1.0 / (1.0 + p * x)
    let y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x)

    return sign * y
}

function cumdev(m, sd, x) {
    return 1 / 2 * (1 + erf((x - m) / (sd * Math.sqrt(2))))
}

function processData(input) {
    let max = 250;
    let n = 100;
    let mu = 2.4;
    let stdev = 2.0;

    console.log(cumdev(n * mu, Math.sqrt(n) * stdev, max).toFixed(4));
} 

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

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