-
Notifications
You must be signed in to change notification settings - Fork 22
/
Geometric_Distribution_II.java
43 lines (37 loc) · 1.08 KB
/
Geometric_Distribution_II.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
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Save input */
Scanner scan = new Scanner(System.in);
int numerator = scan.nextInt();
int denominator = scan.nextInt();
int n = scan.nextInt();
scan.close();
/* Geometric Series */
double p = (double) numerator / denominator;
double result = 0;
for (int i = 1; i <= 5; i++) {
result += geometric(i, p);
}
System.out.format("%.3f", result);
}
private static double geometric (int n, double p) {
return Math.pow(1 - p, n - 1) * p;
}
private static Long combinations(int n, int x) {
if (n < 0 || x < 0 || x > n) {
return null;
}
return factorial(n) / (factorial(x) * factorial(n - x));
}
private static Long factorial(int n) {
if (n < 0) {
return null;
}
long result = 1;
while (n > 0) {
result *= n--;
}
return result;
}
}