This program uses big numbers to compute the odds of winning the grand prize in a lottery. : BigInteger « Data Type « Java






This program uses big numbers to compute the odds of winning the grand prize in a lottery.

   
/*
 This program is a part of the companion code for Core Java 8th ed.
 (http://horstmann.com/corejava)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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.
 * 
 * @version 1.20 2004-02-10
 * @author Cay Horstmann
 */
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-1)*(n-2)*...*(n-k+1)/(1*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!");
  }
}

   
    
    
  








Related examples in the same category

1.BigInteger.isProbablePrime
2.Compute the Palindrome of a number by adding the number composed of
3.Operating with Big Integer Values
4.Create BigInteger via string
5.Create BigInteger via long type variable
6.Multiply one BigInteger to another BigInteger
7.Subtract one BigInteger from another BigInteger
8.Divide one BigInteger from another BigInteger
9.Negate a BigInteger
10.Calculate the power on a BigInteger
11.Create BigInteger from byte array
12.Performing Bitwise Operations with BigInteger
13.Get the value of a bit
14.Set a bit for BigInteger
15.Clear a bit in a BigInteger
16.Flip a bit in a BigInteger
17.Shift the bits in a BigInteger
18.Shift right in a BigInteger
19.xor a BigInteger
20.and operation on BigInteger
21.not operation for BigInteger
22.or operation for BigInteger
23.antNot operation on BigInteger
24.Retrieve the current bits in a byte array in twos-complement form.
25.Parsing and Formatting a Big Integer into Binary
26.Parsing and Formatting a Big Integer into octal
27.Parsing and Formatting a Big Integer into decimal
28.Parse and format to hexadecimal
29.Parse and format to arbitrary radix <= Character.MAX_RADIX
30.Create a BigInteger using the byte array
31.Parsing and Formatting a Byte Array into Binary
32.Parsing and Formatting a Byte Array into Octal
33.Parsing and Formatting a Byte Array into decimal
34.Parsing and Formatting a Byte Array into Hexadecimal
35.Parse binary string
36.Get byte array from BigInteger
37.Parse octal string to create BigInteger
38.Parse decimal string to create BigInteger
39.Parse hexadecimal string to create BigInteger
40.Convert BigInteger into another radix number
41.Do math operation for BigInteger
42.Operate with big integer values in code
43.Demonstration of high-precision integer arithmetic with the BigInteger classDemonstration of high-precision integer arithmetic with the BigInteger class
44.BigInteger bitwise Gcd
45.BigInteger lowest numBits
46.BigInteger highest numBits
47.Is one BigInteger Divisible by another BigInteger
48.Is a BigInteger Even