-
Notifications
You must be signed in to change notification settings - Fork 20k
My Commit for the Issue #2864 Pascal's Triangle #2871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d950cd3
My Commit for the Issue #2864 Pascal's Triangle
hs094 5682151
Update src/main/java/com/thealgorithms/maths/PascalTriangle.java
hs094 7f31005
Update PascalTriangle.java
hs094 c792737
Merge branch 'TheAlgorithms:master' into master
hs094 c698f28
Update PascalTriangle.java
hs094 4f420f9
Create PascalTriangleTest.java
hs094 6cdc4c6
Update PascalTriangleTest.java
hs094 0012b7a
Commit
hs094 f45c226
Commit
hs094 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
My Commit for the Issue #2864 Pascal's Triangle
- Loading branch information
commit d950cd3f7d1617842fc218d882b6fb195cde55e4
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import java.util.Scanner; | ||
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. | ||
* | ||
* 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 | ||
* | ||
* <p> | ||
* Example:- | ||
* 1 | ||
* 1 1 | ||
* 1 2 1 | ||
* 1 3 3 1 | ||
* 1 4 6 4 1 | ||
* 1 5 10 10 5 1 | ||
* 1 6 15 20 15 6 1 | ||
* 1 7 21 35 35 21 7 1 | ||
* 1 8 28 56 70 56 28 8 1 | ||
* | ||
*/ | ||
|
||
public static void main (String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
printPascal(n); | ||
} | ||
|
||
public static void printPascal(int n) | ||
{ | ||
/** | ||
* @param arr An auxiliary array to store generated pascal triangle values | ||
* @return | ||
*/ | ||
int[][] arr = new int[n][n]; | ||
|
||
|
||
/** | ||
* @param line Iterate through every line and print integer(s) in it | ||
* @param i Represents the column number of the element we are currently on | ||
*/ | ||
for (int line = 0; line < n; line++) | ||
{ | ||
/** | ||
* @Every line has number of integers equal to line number | ||
*/ | ||
for (int i = 0; i <= line; i++) | ||
{ | ||
// 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]; | ||
System.out.print(arr[line][i]+" "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
hs094 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.