mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-19 12:24:59 +08:00
Add Pascal's triangle.
This commit is contained in:
39
src/algorithms/math/pascal-triangle/README.md
Normal file
39
src/algorithms/math/pascal-triangle/README.md
Normal 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.
|
||||
|
||||

|
||||
|
||||
## Formula
|
||||
|
||||
The entry in the `nth` row and `kth` column of Pascal's
|
||||
triangle is denoted .
|
||||
For example, the unique nonzero entry in the topmost
|
||||
row is .
|
||||
|
||||
With this notation, the construction of the previous
|
||||
paragraph may be written as follows:
|
||||
|
||||

|
||||
|
||||
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)
|
@ -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]);
|
||||
});
|
||||
});
|
@ -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;
|
||||
}
|
Reference in New Issue
Block a user