-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigIntegerTest.java
33 lines (26 loc) · 969 Bytes
/
BigIntegerTest.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
package BigInterTest;
import java.math.BigInteger;
import java.util.Scanner;
/**
* This program uses big numbers to compute the odds of winning the grand prize in a lottery.
*
* @author Cay Horstman
* @version 1.20 2004-02-10
*/
public class BigIntegerTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("How many numbers do you need to draw? ");
int k = in.nextInt();
System.out.print("What is the highest number you can draw? ");
int n = in.nextInt();
/*
* compute binomial coefficient n*(n-l)*(n-2)*...*(n-k+l)/(l*2*3*...*k)
*/
BigInteger lotteryOdds = BigInteger.valueOf(1);
for (int i = 1; i <= k; i++) {
lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf((n - i + 1)).divide(BigInteger.valueOf(i)));
}
System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
}
}