Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

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

YASH PAL, 31 July 2024

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);
});

10 days of statistics coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes