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.
Each row of Pascal's triangle is generated by iterating through the binomial coefficient function, nCr:
Lets compare this to nCr+1:
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
.