Skip to content

Commit b1242e0

Browse files
hs094siriak
andauthored
Add Pascal's Triangle (TheAlgorithms#2871)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent 734f7a4 commit b1242e0

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.Scanner;
4+
public class PascalTriangle {
5+
/**
6+
*In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that arises
7+
* in probability theory, combinatorics, and algebra. In much of the Western world, it is named after
8+
* the French mathematician Blaise Pascal, although other mathematicians studied it centuries before
9+
* him in India, Persia, China, Germany, and Italy.
10+
*
11+
* The rows of Pascal's triangle are conventionally enumerated starting with row n=0 at the top (the 0th row).
12+
* The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative
13+
* to the numbers in the adjacent rows. The triangle may be constructed in the following manner:
14+
* In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is
15+
* constructed by adding the number above and to the left with the number above and to the right, treating
16+
* blank entries as 0. For example, the initial number in the 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 to produce the number 4 in the fourth row. *
18+
*
19+
*<p>
20+
* link:-https://en.wikipedia.org/wiki/Pascal%27s_triangle
21+
*
22+
* <p>
23+
* Example:-
24+
* 1
25+
* 1 1
26+
* 1 2 1
27+
* 1 3 3 1
28+
* 1 4 6 4 1
29+
* 1 5 10 10 5 1
30+
* 1 6 15 20 15 6 1
31+
* 1 7 21 35 35 21 7 1
32+
* 1 8 28 56 70 56 28 8 1
33+
*
34+
*/
35+
36+
public static int[][] pascal(int n)
37+
{
38+
/**
39+
* @param arr An auxiliary array to store generated pascal triangle values
40+
* @return
41+
*/
42+
int[][] arr = new int[n][n];
43+
/**
44+
* @param line Iterate through every line and print integer(s) in it
45+
* @param i Represents the column number of the element we are currently on
46+
*/
47+
for (int line = 0; line < n; line++)
48+
{
49+
/**
50+
* @Every line has number of integers equal to line number
51+
*/
52+
for (int i = 0; i <= line; i++)
53+
{
54+
// First and last values in every row are 1
55+
if (line == i || i == 0)
56+
arr[line][i] = 1;
57+
// The rest elements are sum of values just above and left of above
58+
else
59+
arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
60+
}
61+
}
62+
63+
return arr;
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
package com.thealgorithms.maths;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class PascalTriangleTest {
9+
10+
@Test
11+
void testForOne()
12+
{
13+
int[][] result = PascalTriangle.pascal(1);
14+
int[][] expected = {{1}};
15+
assertArrayEquals(result,expected);
16+
}
17+
18+
@Test
19+
void testForTwo()
20+
{
21+
int[][] result = PascalTriangle.pascal(2);
22+
int[][] expected = {{1,0},{1,1}};
23+
assertArrayEquals(result,expected);
24+
}
25+
26+
@Test
27+
void testForFive()
28+
{
29+
int[][] result = PascalTriangle.pascal(5);
30+
int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}};
31+
assertArrayEquals(result,expected);
32+
}
33+
34+
@Test
35+
void testForEight() {
36+
int[][] result = PascalTriangle.pascal(8);
37+
int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}};
38+
assertArrayEquals(expected, result);
39+
}
40+
41+
42+
}

src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ void testForOneElement() {
1212
int[] result = ArrayLeftRotation.rotateLeft(arr, 3);
1313
assertArrayEquals(arr, result);
1414
}
15-
15+
1616
@Test
1717
void testForZeroStep() {
1818
int[] arr = {3, 1, 5, 8, 6};
1919
int[] result = ArrayLeftRotation.rotateLeft(arr, 0);
2020
assertArrayEquals(arr, result);
2121
}
22-
22+
2323
@Test
2424
void testForEqualSizeStep() {
2525
int[] arr = {3, 1, 5, 8, 6};
2626
int[] result = ArrayLeftRotation.rotateLeft(arr, 5);
2727
assertArrayEquals(arr, result);
2828
}
29-
29+
3030
@Test
3131
void testForLowerSizeStep() {
3232
int[] arr = {3, 1, 5, 8, 6};
@@ -35,7 +35,7 @@ void testForLowerSizeStep() {
3535
int[] result = ArrayLeftRotation.rotateLeft(arr, n);
3636
assertArrayEquals(expected, result);
3737
}
38-
38+
3939
@Test
4040
void testForHigherSizeStep() {
4141
int[] arr = {3, 1, 5, 8, 6};
@@ -45,4 +45,4 @@ void testForHigherSizeStep() {
4545
assertArrayEquals(expected, result);
4646
}
4747

48-
}
48+
}

0 commit comments

Comments
 (0)