Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a9d1e5

Browse files
Create pascalTraingleRecursive.py
1 parent dc427f6 commit 1a9d1e5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def computeCoeff(row, col):
2+
"""
3+
This method computes the Binomial coefficient for each point in the Pascal Triangle
4+
"""
5+
if col == 0 or row == col:
6+
return 1 # for the corners of each row
7+
else:
8+
return computeCoeff(row-1, col) + computeCoeff(row-1, col-1) # take the numbers in previous row and left of that number
9+
10+
def printTriangle(n):
11+
"""
12+
This method prints the Pascal triangle with `n` rows
13+
"""
14+
for r in range(n):
15+
for c in range(r+1):
16+
print(computeCoeff(r,c), end=' ')
17+
print('\n')
18+
19+
printTriangle(10)
20+
21+
# Output
22+
"""
23+
1
24+
25+
1 1
26+
27+
1 2 1
28+
29+
1 3 3 1
30+
31+
1 4 6 4 1
32+
33+
1 5 10 10 5 1
34+
35+
1 6 15 20 15 6 1
36+
37+
1 7 21 35 35 21 7 1
38+
39+
1 8 28 56 70 56 28 8 1
40+
41+
1 9 36 84 126 126 84 36 9 1
42+
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /