mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-21 11:10:08 +08:00
@ -3,18 +3,19 @@ package com.thealgorithms.maths;
|
||||
public class PascalTriangle {
|
||||
|
||||
/**
|
||||
*In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that arises
|
||||
* in probability theory, combinatorics, and algebra. In much of the Western world, it is named after
|
||||
* the French mathematician Blaise Pascal, although other mathematicians studied it centuries before
|
||||
* him in India, Persia, China, Germany, and Italy.
|
||||
*In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that
|
||||
*arises in probability theory, combinatorics, and algebra. In much of the Western world, it is
|
||||
*named after the French mathematician Blaise Pascal, although other mathematicians studied it
|
||||
*centuries before him in India, Persia, China, Germany, and Italy.
|
||||
*
|
||||
* 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. *
|
||||
* 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. *
|
||||
*
|
||||
*<p>
|
||||
* link:-https://en.wikipedia.org/wiki/Pascal%27s_triangle
|
||||
@ -51,7 +52,8 @@ public class PascalTriangle {
|
||||
// First and last values in every row are 1
|
||||
if (line == i || i == 0) arr[line][i] = 1;
|
||||
// The rest elements are sum of values just above and left of above
|
||||
else arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i];
|
||||
else
|
||||
arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user