Source : https://www.hackerrank.com/challenges/s10-normal-distribution-2
Objective
In this challenge, we go further with normal distributions. We recommend reviewing the previous challenge's Tutorial before attempting this problem.
Task
The final grades for a Physics exam taken by a large group of students have a mean of and a standard deviation of . If we can approximate the distribution of these grades by a normal distribution, what percentage of the students:
- Scored higher than (i.e., have a )?
- Passed the test (i.e., have a )?
- Failed the test (i.e., have a )?
Find and print the answer to each question on a new line, rounded to a scale of decimal places.
Input Format
There are lines of input (shown below):
70 108060
The first line contains space-separated values denoting the respective mean and standard deviation for the exam. The second line contains the number associated with question . The third line contains the pass/fail threshold number associated with questions and .
If you do not wish to read this information from stdin, you can hard-code it into your program.
Output Format
There are three lines of output. Your answers must be rounded to a scale of decimal places (i.e., format):
- On the first line, print the answer to question (i.e., the percentage of students having ).
- On the second line, print the answer to question (i.e., the percentage of students having ).
- On the third line, print the answer to question (i.e., the percentage of students having ).
Source : https://www.hackerrank.com/challenges/s10-normal-distribution-2
Solution
// github.com/RodneyShag | |
public class Solution { | |
public static void main(String[] args) { | |
double mean = 70; | |
double std = 10; | |
double result_1 = 100 * (1 - cumulative(mean, std, 80)); | |
double result_2 = 100 * (1 - cumulative(mean, std, 60)); | |
double result_3 = 100 * cumulative(mean, std, 60); | |
System.out.format("%.2f%n", result_1); | |
System.out.format("%.2f%n", result_2); | |
System.out.format("%.2f%n", result_3); | |
} | |
/* Calculates cumulative probability */ | |
public static double cumulative(double mean, double std, double x) { | |
double parameter = (x - mean) / (std * Math.sqrt(2)); | |
return (0.5) * (1 + erf(parameter)); | |
} | |
/* Source: http://introcs.cs.princeton.edu/java/21function/ErrorFunction.java.html */ | |
// fractional error in math formula less than 1.2 * 10 ^ -7. | |
// although subject to catastrophic cancellation when z in very close to 0 | |
// from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2 | |
public static double erf(double z) { | |
double t = 1.0 / (1.0 + 0.5 * Math.abs(z)); | |
// use Horner's method | |
double ans = 1 - t * Math.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; | |
else return -ans; | |
} | |
} |
No comments:
Post a Comment