home about terms twitter

(Python)Printing binomial theorems and Pascal's triangle

date: 2021-05-01 | mod: 2021-05-01

Introduction

The “Binomial Theorem” is learned elementary algebra, and it has some mathematically interesting properties.

In this article, I consider its application and implementation in Python.

from datetime import datetime as dt
print("Environment")
print("Google Colaboratory Ver:" + str(dt.now().strftime("%Y/%m/%d")))
!python --version
Environment
Google Colaboratory ver:2021/04/17
Python 3.7.10

For react version, please see [(React) Drawing Pascal’s triangle in browser] (Pascal’s Triangle only)

Method

Binomial Theorem

$(a+b)^2 = a^2 + 2ab + b^2$

$(a+b)^3 = a^3 + 3a^2b + 3ab^2 + b^2$

In general, for the expansion (expansion formula) of polynomials like below is valid.

$(a+b)^n = {}_n\mathrm{C}_ra^n + _{n}\mathrm{C}_1a^{n+1}b … _{n}\mathrm{C}_nb^{n}$

This is called the Binomial Theorem.

Related to the binomial theorem, there is an interesting array of numbers called “Pascal’s triangle”.

Pascal’s triangle means that the coefficients of a polynomial when expanded have the relationship with the coefficients of the next polynomial.

First, let’s draw the coefficients of the polynomial.

from math import factorial as fc
# fc(2) = 2!

nmax = 9 # (a+b)^nmax
for n in range(nmax+1):
    b = [r for r in range(n+1)]
    # nCr => fc(n)/(fc(n-r)*fc(r)
    a = [str(int(fc(n)/(fc(n-r)*fc(r)))) for r in b]
    print("n = "+str(n), " ".join(a).center(nmax*4))
    # 4 = magic number
n = 0                  1                  
n = 1                 1 1                 
n = 2                1 2 1                
n = 3               1 3 3 1               
n = 4              1 4 6 4 1              
n = 5            1 5 10 10 5 1            
n = 6           1 6 15 20 15 6 1          
n = 7         1 7 21 35 35 21 7 1         
n = 8        1 8 28 56 70 56 28 8 1       
n = 9     1 9 36 84 126 126 84 36 9 1   

A single column represents a single order.

The coefficients that appear in one order (n) are added together and are shown in the coefficients of the next order (n+1).

And if you line up each of the coefficients, a triangle appears.

Such a triangle is called “Pascal’s triangle”.

Sierpinski’s triangle

If we fill in the odd part of Pascal’s triangle, we get “Sierpinski’s triangle”.

For the code above, let’s try to make it ■ for odd numbers and □ for even numbers.

from math import factorial as fc
# fc(2) = 2!

nmax = 30 # (a+b)^nmax
for n in range(nmax+1):
    b = [r for r in range(n+1)]
    # nCr => fc(n)/(fc(n-r)*fc(r)
    a = [str(int(fc(n)/(fc(n-r)*fc(r)))) for r in b]
    '''-added---------------------------------------'''
    a = [i.replace(str(i), "□")
        if int(i)%2 == 0 else "■" for i in a]
    '''---------------------------------------------'''
    print("n = "+str(n), " ".join(a).center(nmax*3))
    # 3 = magic number
n = 0                                             ■                                             
n = 1                                            ■ ■                                            
n = 2                                           ■ □ ■                                           
n = 3                                          ■ ■ ■ ■                                          
n = 4                                         ■ □ □ □ ■                                         
n = 5                                        ■ ■ □ □ ■ ■                                        
n = 6                                       ■ □ ■ □ ■ □ ■                                       
n = 7                                      ■ ■ ■ ■ ■ ■ ■ ■                                      
n = 8                                     ■ □ □ □ □ □ □ □ ■                                     
n = 9                                    ■ ■ □ □ □ □ □ □ ■ ■                                    
n = 10                                   ■ □ ■ □ □ □ □ □ ■ □ ■                                   
n = 11                                  ■ ■ ■ ■ □ □ □ □ ■ ■ ■ ■                                  
n = 12                                 ■ □ □ □ ■ □ □ □ ■ □ □ □ ■                                 
n = 13                                ■ ■ □ □ ■ ■ □ □ ■ ■ □ □ ■ ■                                
n = 14                               ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■                               
n = 15                              ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■                              
n = 16                             ■ □ □ □ □ □ □ □ □ □ □ □ □ □ □ □ ■                             
n = 17                            ■ ■ □ □ □ □ □ □ □ □ □ □ □ □ □ □ ■ ■                            
n = 18                           ■ □ ■ □ □ □ □ □ □ □ □ □ □ □ □ □ ■ □ ■                           
n = 19                          ■ ■ ■ ■ □ □ □ □ □ □ □ □ □ □ □ □ ■ ■ ■ ■                          
n = 20                         ■ □ □ □ ■ □ □ □ □ □ □ □ □ □ □ □ ■ □ □ □ ■                         
n = 21                        ■ ■ □ □ ■ ■ □ □ □ □ □ □ □ □ □ □ ■ ■ □ □ ■ ■                        
n = 22                       ■ □ ■ □ ■ □ ■ □ □ □ □ □ □ □ □ □ ■ □ ■ □ ■ □ ■                       
n = 23                      ■ ■ ■ ■ ■ ■ ■ ■ □ □ □ □ □ □ □ □ ■ ■ ■ ■ ■ ■ ■ ■                      
n = 24                     ■ □ □ □ □ □ □ □ ■ □ □ □ □ □ □ □ ■ □ □ □ □ □ □ □ ■                     
n = 25                    ■ ■ □ □ □ □ □ □ ■ ■ □ □ □ □ □ □ ■ ■ □ □ □ □ □ □ ■ ■                    
n = 26                   ■ □ ■ □ □ □ □ □ ■ □ ■ □ □ □ □ □ ■ □ ■ □ □ □ □ □ ■ □ ■                   
n = 27                  ■ ■ ■ ■ □ □ □ □ ■ ■ ■ ■ □ □ □ □ ■ ■ ■ ■ □ □ □ □ ■ ■ ■ ■                  
n = 28                 ■ □ □ □ ■ □ □ □ ■ □ □ □ ■ □ □ □ ■ □ □ □ ■ □ □ □ ■ □ □ □ ■                 
n = 29                ■ ■ □ □ ■ ■ □ □ ■ ■ □ □ ■ ■ □ □ ■ ■ □ □ ■ ■ □ □ ■ ■ □ □ ■ ■                
n = 30               ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■ □ ■  

A small triangle is shown within a triangle.

This repeating structure is called a fractal.

Sierpinski’s triangle is also a kind of fractal.