Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/src/main/java/com/thealgorithms/maths/PascalTriangle.java at master · datpp/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
datpp
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
PascalTriangle.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (61 loc) · 2.54 KB
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
PascalTriangle.java
Copy path
Top
File metadata and controls
Code
Blame
65 lines (61 loc) · 2.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package
com
.
thealgorithms
.
maths
;
public
final
class
PascalTriangle
{
private
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
int
[][]
pascal
(
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
;
}
else
{
arr
[
line
][
i
] =
arr
[
line
-
1
][
i
-
1
] +
arr
[
line
-
1
][
i
];
}
}
}
return
arr
;
}
}
You can’t perform that action at this time.