I found a bit of code to obtain Pascal's triangle without using arrays or nCr in Java, given below:
int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++)
{
num = 1;
r = i + 1;
//pre-spacing
for (int j = maxRows - i; j > 0; j--)
{
System.out.print(" ");
}
for (int col = 0; col <= i; col++)
{
if (col > 0)
{
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
For the life of me I can't figure out how this bit of code generates the required number (next in the sequence) :
for (int col = 0; col <= i; col++)
{
if (col > 0)
{
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
Could someone please explain the logic behind how the number is generated? I'm interested in understanding how the formula for the next bit of number is obtained, i.e., how num=num*(r-col)/col
works! I'm also interested in finding out how to derive such a formula.