In this Hackerrank Day 5: Poisson Distribution I 10 Days of Statistics problem A random variable, x, follows a Poisson distribution with a mean of 2.5. Find the probability with which the random variable X is equal to 5.

HackerRank Day 5: Poisson Distribution I | 10 Days of Statistics solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from math import factorial, exp

miu = float(input())
x = int(input())
poisson_prob = ((miu ** x) * exp(-miu)) / factorial(x)
print("%.3f" %poisson_prob)



Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        double lambda = 2.5d;
        double k = 5d;
        DecimalFormat df = new DecimalFormat("0.000");
	    System.out.println(df.format(Poisson(k, lambda)));
    }
    
    public static double Poisson(double k, double lambda){
        return Math.pow(lambda, k) * Math.pow(Math.E, -lambda) / factorial(k);
    }
    
    public static double factorial(double n){
        double x = 1;
        for(double i = 2; i <= n; i++){
            x *= i;
        }
        return x;
    }
}


Problem solution in C++ programming.

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

long Factorial(int n){
    long f = 1;
    if(n != 0){
        for(int i = n; i > 0; i--)
            f *= i;
    }
    else
        f = 1;
    
    return f;
}

double Poisson(double mean, int k){
    long f;
    
    f = Factorial(k);
    return pow(mean, k) * exp(-mean) / f;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    double mean, P;
    int k;
    
    cin >> mean;
    cin >> k;
    
    P = Poisson(mean, k);
    
    cout << fixed << setprecision(3) << P;
    
    return 0;
}


Problem solution in C programming.

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

int main() {
    long fact(int);
    double lambda = 2.5;
    int obser = 5;
    double prob = pow(lambda,obser)*exp(-lambda)/fact(obser);
    printf("%.3lf",prob);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}
long fact(int c){
    if (c==1 || c==0){
        return 1;
    }
    else{
        return c*fact(c-1);
    }
}


Problem solution in JavaScript programming.

function processData(input) {
  //Enter your code here
  var lines = input.split('\n');
  var mean = Number(lines[0]);
  var x = parseInt(lines[1]);
    
  var factorial = function(n) {
      return n > 2 ? n * factorial(n - 1) : (n == 0) ? 1 : n;
  }
  var poissonDistProb = function(lambda, k) {
    return Math.pow(mean, k) * Math.pow(Math.E, (-1) * lambda) / factorial(k);
  }
  var round = function(value, decimals) {
    return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
  }
  
  // Probability of x
  var result = poissonDistProb(mean, x);
    
  console.log(round(result, 3));
} 

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

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