• + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            double mean = 20;
            double stdDeviation = 2;
            double x1 = 19.5, x2 = 22;
    
            double result1 = probability(mean, stdDeviation, x1);
            double probLe20 = 0.5; // le mean == ge mean == 50%
            double result2 = probability(mean, stdDeviation, x2) - probLe20;
    
            System.out.println(round(result1));
            System.out.println(round(result2));
        }
    		
        static double probability(double mean, double stdDeviation, double x) {
            final double z = calcZ(mean, stdDeviation, x);
            return 1 - (1 + erf(z)) / 2;
        }
    		
        static double calcZ(double mean, double stdDeviation, double x) {
            return (mean - x) / (stdDeviation * Math.sqrt(2));
        }
    		
        static double erf(double z) {
            double sum = 0;
            for (int n = 0; n < 11; n++) {
                double t = z / (2 * n + 1);
                for (int i = 1; i <= n; i++)  t *= -z * z / i;
                sum += t;
            }
            return 2 / Math.sqrt(Math.PI) * sum;
        }
    		
        static double round(double v) {
            return Math.round(v * 1000) * 1d / 1000;
        }
    }