Search code examples
javamathpascals-triangle

Pascal's triangle generalized formula derivation


class Solution {
    public static void main(String[] argh) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int number = 1;
            System.out.format("%" + (t - i) * 2 + "s", "");
            for (int j = 0; j <= i; j++) {
                System.out.format("%4d", number);
                // how this formula was derived ???
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

The only thing I want to know is that how the formula for generating each element was derived, it works perfect but how?

number = number * (i - j) / (j + 1)

Just want to derive such expressions in similar questions.


Solution

  • Each row of Pascal's triangle is generated by iterating through the binomial coefficient function, nCr:

    enter image description here

    Lets compare this to nCr+1:

    enter image description here

    The second factor on the second line is exactly the factor (i - j) / (j + 1) which you multiply by to obtain the next number in the row. In the code j = r, i = n.