I'm interested in finding the nth row of pascal triangle (not a specific element but the whole row itself). What would be the most efficient way to do it?
I thought about the conventional way to construct the triangle by summing up the corresponding elements in the row above which would take:
1 + 2 + .. + n = O(n^2)
Another way could be using the combination formula of a specific element:
c(n, k) = n! / (k!(n-k)!)
for each element in the row which I guess would take more time the the former method depending on the way to calculate the combination. Any ideas?
here is a fast example implemented in go-lang that calculates from the outer edges of a row and works it's way to the middle assigning two values with a single calculation...
the output for 20 iterations takes about 1/4 millisecond to run...
I used Ti-84 Plus CE
The use of –> in line 6 is the store value button
List indexes start at 1 so that's why i set it to R+1
This is the fastest way I can think of to do this in programming (with a ti 84) but if you mean to be able to calculate the row using pen and paper then just draw out the triangle cause doing factorals are a pain!
Here is the another best and simple way to design a Pascal Triangle dynamically using VBA.
This uses the following identity:
So you can start with
C(n,0) = 1
and then calculate the rest of the line using this identity, each time multiplying the previous element by(n-k) / (k+1)
.An easy way to calculate it is by noticing that the element of the next row can be calculated as a sum of two consecutive elements in the previous row.
For example
6 = 5 + 1
,15 = 5 + 10
,1 = 1 + 0
and20 = 10 + 10
. This gives a simple algorithm to calculate the next row from the previous one.