1

So, I have implemented the binomial coefficient

public static int binomial(int n, int k) {
    if (k == 0)
        return 1;
    else if (k > n - k)
        return binomial(n, n - k);
    else
        return binomial(n - 1, k - 1) * n / k;
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Insert n: ");
    int n = scan.nextInt();

    System.out.println("Insert k: ");
    int k = scan.nextInt();

    System.out.println("Result: " + binomial(n, k));
}

And it works, but where I'm stuck is just that I need to add the coefficient array for two given numbers. So If n is 5 and k is 3. The coefficient array will display: 1 5 10 10. Any ideas?

idkrlly
  • 13
  • 4

3 Answers3

2

Don't call recursive code in a for loop. That adds a stupid amount of redundancy.

Pass the array as a parameter to the recursive function from main. Arrays are passed by reference in java.

public static int binomial(int n, int k, int[] coefficient) {
    int ret;
    if (k == 0) {
        ret = 1;
    } else if (k > n - k) {
        ret = binomial(n, n - k, coefficient);
    } else {
        ret = binomial(n - 1, k - 1, coefficient) * n / k;
    }
    coefficient[k] = ret;
    return ret;
}
Community
  • 1
  • 1
0

All you need to do is put your expression in a loop and hold n constant.

for (int k = 0; k <= n; k++) {
    System.out.print(binomial(n, k) + " ");
}

You can store these values in an array if you like. There is no need to make your method any more complicated.

If want to put it in an array, here is one easy way to do it.

int coefs[] = IntStream.rangeClosed(0, n).map(k -> binomial(n, k)).toArray();
coefs[] = [1, 5, 10, 10, 5, 1]
Community
  • 1
  • 1
WJS
  • 36,363
  • 4
  • 24
  • 39
0

You can create two iterative methods: one returns a 2d array containing Pascal's triangle, and the second returns the base of that triangle. It is more useful for clarity.

Output:

Insert n:
6
Pascal's triangle:
[1, 1, 1, 1, 1, 1]
[1, 2, 3, 4, 5]
[1, 3, 6, 10]
[1, 4, 10]
[1, 5]
[1]
Binomial coefficients:
[1, 5, 10, 10, 5, 1]

Code:

public static int[][] binomialTriangle(int n) {
    // an array of 'n' rows
    int[][] arr = new int[n][];
    // iterate over the rows of the array
    IntStream.range(0, n).forEach(i -> {
        // a row of 'n-i' elements
        arr[i] = new int[n - i];
        // iterate over the columns of the array
        IntStream.range(0, n - i).forEach(j -> {
            if (i == 0 || j == 0)
                // first row and column
                // are filled with ones
                arr[i][j] = 1;
            else
                // all other elements are the sum of the
                // previous element in the row and column
                arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
        });
    });
    return arr;
}
public static int[] binomialCoefficient(int[][] triangle) {
    return Arrays.stream(triangle)
            // the last element in the row
            .mapToInt(row -> row[row.length - 1])
            .toArray();
}
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Insert n:");
    int n = scan.nextInt();

    System.out.println("Pascal's triangle:");
    int[][] arr = binomialTriangle(n);
    Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);

    int[] base = binomialCoefficient(arr);
    System.out.println("Binomial coefficients:");
    System.out.println(Arrays.toString(base));
}

See also: Convert negative index to positive index in an array (Trinomial Triangle)