Skip to content

Commit f3189cc

Browse files
committed
Add Pascal's triangle.
1 parent 92a9060 commit f3189cc

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

‎README.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,26 @@ a set of rules that precisely define a sequence of operations.
5757
*`B`[Primality Test](src/algorithms/math/primality-test) (trial division method)
5858
*`B`[Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
5959
*`B`[Least Common Multiple](src/algorithms/math/least-common-multiple) (LCM)
60-
*`A`[Integer Partition](src/algorithms/math/integer-partition)
6160
*`B`[Sieve of Eratosthenes](src/algorithms/math/sieve-of-eratosthenes) - finding all prime numbers up to any given limit
6261
*`B`[Is Power of Two](src/algorithms/math/is-power-of-two) - check if the number is power of two (naive and bitwise algorithms)
62+
*`B`[Pascal's Triangle](src/algorithms/math/pascal-triangle)
63+
*`A`[Integer Partition](src/algorithms/math/integer-partition)
6364
*`A`[Liu Hui π Algorithm](src/algorithms/math/liu-hui) - approximate π calculations based on N-gons
6465
***Sets**
6566
*`B`[Cartesian Product](src/algorithms/sets/cartesian-product) - product of multiple sets
67+
*`B`[Fisher–Yates Shuffle](src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
6668
*`A`[Power Set](src/algorithms/sets/power-set) - all subsets of a set
6769
*`A`[Permutations](src/algorithms/sets/permutations) (with and without repetitions)
6870
*`A`[Combinations](src/algorithms/sets/combinations) (with and without repetitions)
69-
*`B`[Fisher–Yates Shuffle](src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
7071
*`A`[Longest Common Subsequence](src/algorithms/sets/longest-common-subsequence) (LCS)
7172
*`A`[Longest Increasing Subsequence](src/algorithms/sets/longest-increasing-subsequence)
7273
*`A`[Shortest Common Supersequence](src/algorithms/sets/shortest-common-supersequence) (SCS)
7374
*`A`[Knapsack Problem](src/algorithms/sets/knapsack-problem) - "0/1" and "Unbound" ones
7475
*`A`[Maximum Subarray](src/algorithms/sets/maximum-subarray) - "Brute Force" and "Dynamic Programming" (Kadane's) versions
7576
*`A`[Combination Sum](src/algorithms/sets/combination-sum) - find all combinations that form specific sum
7677
***Strings**
77-
*`A`[Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
7878
*`B`[Hamming Distance](src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
79+
*`A`[Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
7980
*`A`[Knuth–Morris–Pratt Algorithm](src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search (pattern matching)
8081
*`A`[Z Algorithm](src/algorithms/string/z-algorithm) - substring search (pattern matching)
8182
*`A`[Rabin Karp Algorithm](src/algorithms/string/rabin-karp) - substring search
@@ -100,11 +101,11 @@ a set of rules that precisely define a sequence of operations.
100101
***Graphs**
101102
*`B`[Depth-First Search](src/algorithms/graph/depth-first-search) (DFS)
102103
*`B`[Breadth-First Search](src/algorithms/graph/breadth-first-search) (BFS)
104+
*`B`[Kruskal’s Algorithm](src/algorithms/graph/kruskal) - finding Minimum Spanning Tree (MST) for weighted undirected graph
103105
*`A`[Dijkstra Algorithm](src/algorithms/graph/dijkstra) - finding shortest path to all graph vertices
104106
*`A`[Bellman-Ford Algorithm](src/algorithms/graph/bellman-ford) - finding shortest path to all graph vertices
105107
*`A`[Detect Cycle](src/algorithms/graph/detect-cycle) - for both directed and undirected graphs (DFS and Disjoint Set based versions)
106108
*`A`[Prim’s Algorithm](src/algorithms/graph/prim) - finding Minimum Spanning Tree (MST) for weighted undirected graph
107-
*`B`[Kruskal’s Algorithm](src/algorithms/graph/kruskal) - finding Minimum Spanning Tree (MST) for weighted undirected graph
108109
*`A`[Topological Sorting](src/algorithms/graph/topological-sorting) - DFS method
109110
*`A`[Articulation Points](src/algorithms/graph/articulation-points) - Tarjan's algorithm (DFS based)
110111
*`A`[Bridges](src/algorithms/graph/bridges) - DFS based algorithm
@@ -114,9 +115,9 @@ a set of rules that precisely define a sequence of operations.
114115
*`A`[Travelling Salesman Problem](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
115116
***Uncategorized**
116117
*`B`[Tower of Hanoi](src/algorithms/uncategorized/hanoi-tower)
118+
*`B`[Square Matrix Rotation](src/algorithms/uncategorized/square-matrix-rotation) - in-place algorithm
117119
*`A`[N-Queens Problem](src/algorithms/uncategorized/n-queens)
118120
*`A`[Knight's Tour](src/algorithms/uncategorized/knight-tour)
119-
*`B`[Square Matrix Rotation](src/algorithms/uncategorized/square-matrix-rotation) - in-place algorithm
120121

121122
### Algorithms by Paradigm
122123

@@ -135,13 +136,14 @@ algorithm is an abstraction higher than a computer program.
135136
***Divide and Conquer** - divide the problem into smaller parts and then solve those parts
136137
*`B`[Binary Search](src/algorithms/search/binary-search)
137138
*`B`[Tower of Hanoi](src/algorithms/uncategorized/hanoi-tower)
139+
*`B`[Pascal's Triangle](src/algorithms/math/pascal-triangle)
138140
*`B`[Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
139-
*`A`[Permutations](src/algorithms/sets/permutations) (with and without repetitions)
140-
*`A`[Combinations](src/algorithms/sets/combinations) (with and without repetitions)
141141
*`B`[Merge Sort](src/algorithms/sorting/merge-sort)
142142
*`B`[Quicksort](src/algorithms/sorting/quick-sort)
143143
*`B`[Tree Depth-First Search](src/algorithms/tree/depth-first-search) (DFS)
144144
*`B`[Graph Depth-First Search](src/algorithms/graph/depth-first-search) (DFS)
145+
*`A`[Permutations](src/algorithms/sets/permutations) (with and without repetitions)
146+
*`A`[Combinations](src/algorithms/sets/combinations) (with and without repetitions)
145147
***Dynamic Programming** - build up a solution using previously found sub-solutions
146148
*`B`[Fibonacci Number](src/algorithms/math/fibonacci)
147149
*`A`[Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Pascal's Triangle
2+
3+
In mathematics, **Pascal's triangle** is a triangular array of
4+
the binomial coefficients.
5+
6+
The rows of Pascal's triangle are conventionally enumerated
7+
starting with row `n = 0` at the top (the `0th` row). The
8+
entries in each row are numbered from the left beginning
9+
with `k = 0` and are usually staggered relative to the
10+
numbers in the adjacent rows. The triangle may be constructed
11+
in the following manner: In row `0` (the topmost row), there
12+
is a unique nonzero entry `1`. Each entry of each subsequent
13+
row is constructed by adding the number above and to the
14+
left with the number above and to the right, treating blank
15+
entries as `0`. For example, the initial number in the
16+
first (or any other) row is `1` (the sum of `0` and `1`),
17+
whereas the numbers `1` and `3` in the third row are added
18+
to produce the number `4` in the fourth row.
19+
20+
![Pascal's Triangle](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
21+
22+
## Formula
23+
24+
The entry in the `nth` row and `kth` column of Pascal's
25+
triangle is denoted ![Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/206415d3742167e319b2e52c2ca7563b799abad7).
26+
For example, the unique nonzero entry in the topmost
27+
row is ![Formula example](https://wikimedia.org/api/rest_v1/media/math/render/svg/b7e35f86368d5978b46c07fd6dddca86bd6e635c).
28+
29+
With this notation, the construction of the previous
30+
paragraph may be written as follows:
31+
32+
![Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/203b128a098e18cbb8cf36d004bd7282b28461bf)
33+
34+
for any non-negative integer `n` and any
35+
integer `k` between `0` and `n`, inclusive.
36+
37+
## References
38+
39+
-[Wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
importpascalTriangleRecursivefrom'../pascalTriangleRecursive';
2+
3+
describe('pascalTriangleRecursive',()=>{
4+
it('should calculate Pascal Triangle coefficients for specific line number',()=>{
5+
expect(pascalTriangleRecursive(0)).toEqual([1]);
6+
expect(pascalTriangleRecursive(1)).toEqual([1,1]);
7+
expect(pascalTriangleRecursive(2)).toEqual([1,2,1]);
8+
expect(pascalTriangleRecursive(3)).toEqual([1,3,3,1]);
9+
expect(pascalTriangleRecursive(4)).toEqual([1,4,6,4,1]);
10+
expect(pascalTriangleRecursive(5)).toEqual([1,5,10,10,5,1]);
11+
expect(pascalTriangleRecursive(6)).toEqual([1,6,15,20,15,6,1]);
12+
expect(pascalTriangleRecursive(7)).toEqual([1,7,21,35,35,21,7,1]);
13+
});
14+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param{number} lineNumber
3+
* @return{number[]}
4+
*/
5+
exportdefaultfunctionpascalTriangleRecursive(lineNumber){
6+
if(lineNumber===0){
7+
return[1];
8+
}
9+
10+
constcurrentLineSize=lineNumber+1;
11+
constpreviousLineSize=currentLineSize-1;
12+
13+
// Create container for current line values.
14+
constcurrentLine=[];
15+
16+
// We'll calculate current line based on previous one.
17+
constpreviousLine=pascalTriangleRecursive(lineNumber-1);
18+
19+
// Let's go through all elements of current line except the first and
20+
// last one (since they were and will be filled with 1's) and calculate
21+
// current coefficient based on previous line.
22+
for(letnumIndex=0;numIndex<currentLineSize;numIndex+=1){
23+
constleftCoefficient=(numIndex-1)>=0 ? previousLine[numIndex-1] : 0;
24+
constrightCoefficient=numIndex<previousLineSize ? previousLine[numIndex] : 0;
25+
26+
currentLine[numIndex]=leftCoefficient+rightCoefficient;
27+
}
28+
29+
returncurrentLine;
30+
}

0 commit comments

Comments
(0)