/home/wpollock1/public_html/AJava/BigNum.java
// Demo of using BigInteger and BigDecimal. Points to note:
// BigInteger doesn't provide an int or long constructor, so the
// static valueOf method is used.
// BigDecimal does have a double constructor, but it should never be used!
// There is also a BigDecimal constructor that uses a BigInteger.
//
// Written 8/2005 by Wayne Pollock, Tampa Florida USA.
// Updated 2022 by Wayne Pollock, for Java language changes.
import java.math.*;
import static java.math.BigInteger.*;
public class BigNum
{
public static void main ( String [] args )
{
int digit = 7; // a randomly chosen digit.
BigInteger bi = valueOf( 12345679 ); // Note use of static import
BigInteger nine = new BigInteger( "9" ); // Note string constructor.
bi = bi.multiply( nine );
bi = bi.multiply( BigInteger.valueOf( digit ) );
System.out.println( "the result is: " + bi );
BigDecimal num = BigDecimal.ZERO;
num = num.add( BigDecimal.valueOf( 3.14 ) );
num = num.divide( BigDecimal.valueOf( 2 ), RoundingMode.HALF_EVEN );
System.out.println( "pi/2 = " + num );
// Calculate odds of winning Florida lotto (pick 6 of 53 numbers):
// Formula for choosing 6 of 53 is: C(53, 6) = 53!/(6! * (53-6)!)
System.out.println( "\nFlorida Lotto Odds of Winning\n" );
final int possibleValues = 53;
final int numToPick = 6;
BigInteger numerator = factorial( valueOf(possibleValues) );
BigInteger denominator = factorial(valueOf(numToPick)).multiply(
factorial(valueOf(possibleValues-numToPick)) );
System.out.println( "numerator = " + numerator );
System.out.println( "demominator = " + denominator );
BigInteger odds = numerator.divide( denominator );
System.out.println( "Odds of winning Florida lotto: 1 in " + odds );
}
/** Computes num! = num * (num-1) * (num-2) * ... * 3 * 2 * 1
*/
static BigInteger factorial ( BigInteger num )
{
if ( num.equals(ONE) ) // notice static import use!
return ONE;
return num.multiply( factorial( num.subtract(ONE) ) );
}
}