COSC 126 - Data Structures and Algorithms 2

Assignment #12. Due Date: Mon, April 11

Write a Java program which uses the BigInteger class to compute binomial coefficients with arbitrary precision. Your program should read in two BigIntegers n, and k and then compute the coefficient n choose k and print the answer.

Your program should use a recursive function implementing the basic algorithm:

  BinomCoeff(n, k)
    if k = 0 or k = n
      return 1
    else
      return BinomCoeff(n-1, k-1) + BinomCoeff(n-1, k)

Once your program is running, experiment to see how large n and k can get until the program takes too long to run.

You can read about BigInteger in the Java API at

http://java.sun.com/j2se/1.5.0/docs/api/

I've also written the following short program to show you how the class works.

//Show a bit of how to use the BigInteger class in Java

import java.math.BigInteger;

class BigIntDemo {

   public static void main(String[] args) {
	BigInteger a,b,c;

	a = new BigInteger("12341234");
	b = new BigInteger("56785678");
	c = a.multiply(b);
	System.out.println("Product = "+c);

	b = a.add(new BigInteger("50"));
        for( BigInteger i = a; i.compareTo(b)<0; i = i.add(new BigInteger("1")))
		System.out.println(i);
   }
}