0% found this document useful (0 votes)
27 views

Pascal Tiangle Python

The document contains three Pascal's triangle programs in Python. The first program uses math.factorial to calculate binomial coefficients. The second program calculates coefficients using factorials and integer division. The third program builds the triangle using a 2D list.

Uploaded by

vsnpradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Pascal Tiangle Python

The document contains three Pascal's triangle programs in Python. The first program uses math.factorial to calculate binomial coefficients. The second program calculates coefficients using factorials and integer division. The third program builds the triangle using a 2D list.

Uploaded by

vsnpradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Pascal 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

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1


Pascal Program 1:

from math import factorial

n = 13

for i in range(n):

for j in range(n-i+1):

# for left spacing

print(end=" ")

for j in range(i+1):

# nCr = n!/((n-r)!*r!)

print(f'{factorial(i)//(factorial(j)*factorial(i-j)):^6d}', end="")

# for new line

print()
Pascal 2:

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

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1


Pascal Program 2:

n = 13

for i in range(1, n+1):

for j in range(0, n-i+1):

print(' ', end='')

# first element is always 1

C=1

for j in range(1, i+1):

# first value in a line is always 1

print(f'{C:^6d}', sep='', end='')

# using Binomial Coefficient

C = C * (i - j) // j

print()
Pascal 3:

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

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1


Pascal Program 3:

n = 12

list1 = []

for i in range(n):

list1.append([])

list1[i].append(1)

for j in range(1,i):

list1[i].append(list1[i-1][j-1] + list1[i-1][j])

if i != 0:

list1[i].append(1)

for i in range(n):

print(' ' * (n-i),end='',sep='')

for j in range(0,i+1):

print('{0:^6d}'.format(list1[i][j]),end='',sep='')

print()

You might also like