if row = 0 or col = 0 or row = col then 1 else P(row, col) = P(row - 1, col - 1) + P(row - 1, col)
The two elements on the row above and columns directly adjacent to the current cell are added giving us a new element.
0 | 1
1 | 1 1
2 | 1 2 1
3 | 1 3 3 1
4 | 1 4 6 4 1
----------
0 1 2 3 4
P (2,1) = P(1,0) + P(1,1) = 1 + 1 = 2
In tacit J:
pasc=: (-&1 1 +&$: -&1 0)`1:@.(=/ +. 0&=)
pasc 2 1
2
An alternative way of calculating pascal's triangle is to use the iteration operator:
pasc2=: 3 : '(0&, + ,&0)^:(i.y)1'
pasc2 5
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
Using the binomial coefficient is the best way, following directly from the definition of pascal's triangle:
pasc3=: [: !/~ i.
pasc3 5
1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1
An interesting property of pascal's triangle is that the diagonals add up to the fibonacci numbers:
</. pasc3 5
+-+---+-----+-------+---------+-------+-----+---+-+
|1|1 0|1 1 0|1 2 0 0|1 3 1 0 0|4 3 0 0|6 1 0|4 0|1|
+-+---+-----+-------+---------+-------+-----+---+-+
({. [: +//. pasc3) 10
1 1 2 3 5 8 13 21 34 55