javaalgorithmbinomial-theorem

Whats wrong in this Binomial Theorem calculator?


After thinking about for 1 hour I am still not able to figure out whats the problem with my calculator. I have made 3 function which include main(), calculateBinomialTheorem() and factorial(). Yes, factorial() to calculate the coefficient.

public static void main(String[] args) {

    Scanner a_input = new Scanner(System.in);
    Scanner b_input = new Scanner(System.in);
    Scanner n_input = new Scanner(System.in);

    int a = 0;
    int b = 0;
    int n = 0;

    System.out.println("Welcome to Binomial Theorem Solver:");

    System.out.print("a: ");
    a = a_input.nextInt();

    System.out.print("b: ");
    b = b_input.nextInt();

    System.out.print("n: ");
    n = n_input.nextInt();

    System.out.print(calculateBinomialTheorem(a, b, n));

    a_input.close();
    b_input.close();
    n_input.close();
}

private static int calculateBinomialTheorem(int a, int b, int n) {
    int result = 0;
    int coefficient = 0;

    ArrayList<Integer> products = new ArrayList<Integer>();

    for(int i = 1; i <= n; i++) {

        int product = 0;

        coefficient = factorial(n) / (factorial(i) * factorial(n - i));
        product = (int) (coefficient*Math.pow(a, n - i)*Math.pow(b, i));

        products.add(product);
    }

    for(int c : products) {
        result += c;
    }

    return result;
}

private static int factorial(int num) {
    int factorial = 1;

    if(num > 0) {
        for ( int c = 1 ; c <= num ; c++ )
            factorial = factorial*c;
    } else {
        return 0;
    }

    return factorial;
}

I tried to run it with the values of 3, 3, 3 that should give me the answer of 216 but its not giving! Why? Every time I run it with those values this is the error that I get:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at binomial_thorem_solver.Main.calculateBinomialTheorem(Main.java:46)
    at binomial_thorem_solver.Main.main(Main.java:29)

I know that I am dividing the number by 0 but I am not getting how to resolve that issue.

Please help.

UPDATE: Thanks for the answers. You all figured out what the problem was but then there was another problem aswell that the loop was iterating one less time because i waas initially set to 1. I set that to 0 and it worked!


Solution

  • 0! = 1 by convention. Not 0. This might cause problem to you.

    Moreover, for loop should go from 0 to n, not from 1 to n as there are n+1 terms.

    You are missing C(n,0)*a^0*b^n part as your iteration is not going from 0 to n.

    So, your loop should be

    for(int i = 0; i <= n; i++) {
    
        int product = 0;
    
        coefficient = factorial(n) / (factorial(i) * factorial(n - i));
        product = (int) (coefficient*Math.pow(a, n - i)*Math.pow(b, i));
    
        products.add(product);
    }
    

    In your case, since C(3,0)*3^0*3^3 that is 27 is missing from the final product. That is why you are getting 216 - 27 = 189.