-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution.java
43 lines (37 loc) · 1.59 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* DeveloperName(): Jignesh Chudasama
* GithubName(): https://github.com/Jignesh-81726
*/
public class Solution {
public static void main(String[] args) {
double ticketsLeft = 250;
int n = 100;
double mean = 2.4;
double std = 2;
/* Formulas are from problem's tutorial */
double samplesMean = n * mean;
double samplesSTD = Math.sqrt(n) * std;
System.out.format("%.4f", cumulative(samplesMean, samplesSTD, ticketsLeft));
}
/* 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));
}
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;
}
}