Add Pascal's triangle.

This commit is contained in:
Oleksii Trekhleb
2018-07-07 10:35:37 +03:00
parent 92a90606dc
commit f3189cca43
4 changed files with 92 additions and 7 deletions

View File

@ -0,0 +1,39 @@
# Pascal's Triangle
In mathematics, **Pascal's triangle** is a triangular array of
the binomial coefficients.
The rows of Pascal's triangle are conventionally enumerated
starting with row `n = 0` at the top (the `0th` row). The
entries in each row are numbered from the left beginning
with `k = 0` and are usually staggered relative to the
numbers in the adjacent rows. The triangle may be constructed
in the following manner: In row `0` (the topmost row), there
is a unique nonzero entry `1`. Each entry of each subsequent
row is constructed by adding the number above and to the
left with the number above and to the right, treating blank
entries as `0`. For example, the initial number in the
first (or any other) row is `1` (the sum of `0` and `1`),
whereas the numbers `1` and `3` in the third row are added
to produce the number `4` in the fourth row.
![Pascal's Triangle](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
## Formula
The entry in the `nth` row and `kth` column of Pascal's
triangle is denoted ![Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/206415d3742167e319b2e52c2ca7563b799abad7).
For example, the unique nonzero entry in the topmost
row is ![Formula example](https://wikimedia.org/api/rest_v1/media/math/render/svg/b7e35f86368d5978b46c07fd6dddca86bd6e635c).
With this notation, the construction of the previous
paragraph may be written as follows:
![Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/203b128a098e18cbb8cf36d004bd7282b28461bf)
for any non-negative integer `n` and any
integer `k` between `0` and `n`, inclusive.
## References
- [Wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle)

View File

@ -0,0 +1,14 @@
import pascalTriangleRecursive from '../pascalTriangleRecursive';
describe('pascalTriangleRecursive', () => {
it('should calculate Pascal Triangle coefficients for specific line number', () => {
expect(pascalTriangleRecursive(0)).toEqual([1]);
expect(pascalTriangleRecursive(1)).toEqual([1, 1]);
expect(pascalTriangleRecursive(2)).toEqual([1, 2, 1]);
expect(pascalTriangleRecursive(3)).toEqual([1, 3, 3, 1]);
expect(pascalTriangleRecursive(4)).toEqual([1, 4, 6, 4, 1]);
expect(pascalTriangleRecursive(5)).toEqual([1, 5, 10, 10, 5, 1]);
expect(pascalTriangleRecursive(6)).toEqual([1, 6, 15, 20, 15, 6, 1]);
expect(pascalTriangleRecursive(7)).toEqual([1, 7, 21, 35, 35, 21, 7, 1]);
});
});

View File

@ -0,0 +1,30 @@
/**
* @param {number} lineNumber
* @return {number[]}
*/
export default function pascalTriangleRecursive(lineNumber) {
if (lineNumber === 0) {
return [1];
}
const currentLineSize = lineNumber + 1;
const previousLineSize = currentLineSize - 1;
// Create container for current line values.
const currentLine = [];
// We'll calculate current line based on previous one.
const previousLine = pascalTriangleRecursive(lineNumber - 1);
// Let's go through all elements of current line except the first and
// last one (since they were and will be filled with 1's) and calculate
// current coefficient based on previous line.
for (let numIndex = 0; numIndex < currentLineSize; numIndex += 1) {
const leftCoefficient = (numIndex - 1) >= 0 ? previousLine[numIndex - 1] : 0;
const rightCoefficient = numIndex < previousLineSize ? previousLine[numIndex] : 0;
currentLine[numIndex] = leftCoefficient + rightCoefficient;
}
return currentLine;
}