In this Hackerrank Day 5: Normal Distribution II 10 Days of Statistics problem in the final grades for a physical exam taken by a large group of students have a mean of 70 and a standard deviation of 10. if we can approximate the distribution of these grades by a normal distribution, what percentage of the students

  1. Score higher than 80.
  2. Passed the test if the grade > 60.
  3. Failed the test if the grade < 60.

HackerRank Day 5: Normal Distribution II | 10 Days of Statistics solution


Problem solution in Python programming.

import math
mean, std = 70, 10
cdf = lambda x: 0.5 * (1 + math.erf((x - mean) / (std * (2 ** 0.5))))


print(round((1-cdf(80))*100,2))
print(round((1-cdf(60))*100,2))
print(round((cdf(60))*100,2))



Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
      double u = 70.0;
		double o = 10.0;
		double a = 80.0;
		double b = 60.0;
		
		double r1 = 1 - 0.5*(1+erf((a-u)/(o*Math.sqrt(2))));
		
		
		double r3 = 0.5*(1+erf((b-u)/(o*Math.sqrt(2))));
		double r2 = 1 - r3;
		
        System.out.println(String.format( "%.2f", r1*100));
        System.out.println(String.format( "%.2f", r2*100));
        System.out.println(String.format( "%.2f", r3*100));

    }

	
	public static double erf(double x) {
        // constants
        final double a1 =  0.254829592;
        final double a2 = -0.284496736;
        final double a3 =  1.421413741;
        final double a4 = -1.453152027;
        final double a5 =  1.061405429;
        final double p  =  0.3275911;

        // Save the sign of x
        double sign = 1;
        if (x < 0) {
            sign = -1;
        }
        x = Math.abs(x);

        // A&S formula 7.1.26
        double t = 1.0/(1.0 + p*x);
        double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*Math.exp(-x*x);

        return sign*y;
    }

}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
double func(double x, double mu, double sd) {
    double a = sqrt(sd)*sqrt(2.0*M_PI);
    double b = -1*pow(x-mu,2.0)/(2.0*sd);
    return (1 / a)*pow(2.71828,b);
}
*/
double erf(double z) {
        double t = 1.0 / (1.0 + 0.5 * abs(z));

        // use Horner's method
        double ans = 1 - t * exp( -z*z   -   1.26551223 +
                                            t * ( 1.00002368 +
                                            t * ( 0.37409196 + 
                                            t * ( 0.09678418 + 
                                            t * (-0.18628806 + 
                                            t * ( 0.27886807 + 
                                            t * (-1.13520398 + 
                                            t * ( 1.48851587 + 
                                            t * (-0.82215223 + 
                                            t * ( 0.17087277))))))))));
        if (z >= 0) return  ans;
        
        return -1*ans;
}

double func(double x, double mu, double sd) {
    return 0.5 * ( 1 + erf((x - mu)/(sqrt(2)*sd)) );
}

int main() {
    double mu = 70.0;
    double sd = 10.0;
    
    double b = func(80.0,mu,sd);
    double c = func(60.0,mu,sd);
    
    printf("%0.2f\n",100.0*(1-b));
    printf("%0.2f\n",100.0*(1-c));
    printf("%0.2f\n",100.0*c);
    
    return 0;
}


Problem solution in C programming.

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


// F_x (X <= x)
double cumulative_distribution (double x, double mean, double variance) {
    static const double sqrt_2 = sqrt (2);
    static const double one_half = 1.0 / 2.0;
    
    return one_half * (1 + erf ((x - mean) / (sqrt (variance) * sqrt_2)));
}


int main () {
    double mean, variance;
    double grade_1, grade_2;
    scanf ("%lf %lf", &mean, &variance);
    scanf ("%lf %lf", &grade_1, &grade_2);
    variance *= variance;
    
    printf ("%.2lf\n%.2lf\n%.2lf\n",
           100.0 * (1.0 - cumulative_distribution (grade_1, mean, variance)),
           100.0 * (1.0 - cumulative_distribution (grade_2, mean, variance)),
           100.0 * (cumulative_distribution (grade_2, mean, variance)));
    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    function erf(x) {
        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 cumulativeDistribution(m, sd, x) {
        return 1 / 2 * (1 + erf((x - m) / (sd * Math.sqrt(2))));
    }

    input = input.split('\n');
    input[0] = input[0].split(' ');

    let m = parseFloat(input[0][0]),
        sd = parseFloat(input[0][1]),
        a = parseFloat(input[1]),
        bc = parseFloat(input[2]);

    console.log(((1 - cumulativeDistribution(m, sd, a)) * 100).toFixed(2));
    console.log(((1 - cumulativeDistribution(m, sd, bc)) * 100).toFixed(2));
    console.log(((cumulativeDistribution(m, sd, bc)) * 100).toFixed(2));
} 

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

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