|
1 | 1 | # Pascal's Triangle
|
2 | 2 |
|
3 | 3 | In mathematics, **Pascal's triangle** is a triangular array of
|
4 |
| -the binomial coefficients. |
| 4 | +the [binomial coefficients](https://en.wikipedia.org/wiki/Binomial_coefficient). |
5 | 5 |
|
6 | 6 | The rows of Pascal's triangle are conventionally enumerated
|
7 | 7 | starting with row `n = 0` at the top (the `0th` row). The
|
@@ -34,6 +34,31 @@ paragraph may be written as follows:
|
34 | 34 | for any non-negative integer `n` and any
|
35 | 35 | integer `k` between `0` and `n`, inclusive.
|
36 | 36 |
|
| 37 | + |
| 38 | + |
| 39 | +## Calculating triangle entries in O(n) time |
| 40 | + |
| 41 | +We know that `i`-th entry in a line number `lineNumber` is |
| 42 | +Binomial Coefficient `C(lineNumber, i)` and all lines start |
| 43 | +with value `1`. The idea is to |
| 44 | +calculate `C(lineNumber, i)` using `C(lineNumber, i-1)`. It |
| 45 | +can be calculated in `O(1)` time using the following: |
| 46 | + |
| 47 | +``` |
| 48 | +C(lineNumber, i) = lineNumber! / ((lineNumber - i)! * i!) |
| 49 | +C(lineNumber, i - 1) = lineNumber! / ((lineNumber - i + 1)! * (i - 1)!) |
| 50 | +``` |
| 51 | + |
| 52 | +We can derive following expression from above two expressions: |
| 53 | + |
| 54 | +``` |
| 55 | +C(lineNumber, i) = C(lineNumber, i - 1) * (lineNumber - i + 1) / i |
| 56 | +``` |
| 57 | + |
| 58 | +So `C(lineNumber, i)` can be calculated |
| 59 | +from `C(lineNumber, i - 1)` in `O(1)` time. |
| 60 | + |
37 | 61 | ## References
|
38 | 62 |
|
39 | 63 | - [Wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle)
|
| 64 | +- [GeeksForGeeks](https://www.geeksforgeeks.org/pascal-triangle/) |
0 commit comments