Source : https://www.hackerrank.com/challenges/s10-poisson-distribution-1
Objective
In this challenge, we learn about Poisson distributions. Check out the Tutorial tab for learning materials!
Task
A random variable, , follows Poisson distribution with mean of . Find the probability with which the random variable is equal to .
Input Format
The first line contains 's mean. The second line contains the value we want the probability for:
2.55
If you do not wish to read this information from stdin, you can hard-code it into your program.
Output Format
Print a single line denoting the answer, rounded to a scale of decimal places (i.e., format).
Source : https://www.hackerrank.com/challenges/s10-poisson-distribution-1
Solution
// github.com/RodneyShag | |
import java.util.Scanner; | |
public class Solution { | |
public static void main(String[] args) { | |
/* Save input */ | |
Scanner scan = new Scanner(System.in); | |
double lambda = scan.nextDouble(); | |
int k = scan.nextInt(); | |
scan.close(); | |
System.out.println(poisson(k, lambda)); | |
} | |
private static double poisson(int k, double lambda) { | |
return (Math.pow(lambda, k) * Math.pow(Math.E, -1 * lambda)) / factorial(k); | |
} | |
private static Long factorial(int n) { | |
if (n < 0) { | |
return null; | |
} | |
long result = 1; | |
while (n > 0) { | |
result *= n--; | |
} | |
return result; | |
} | |
} |
No comments:
Post a Comment