[python]Three ways to implement Yang Hui's triangle in Python

1. Introduction to Yang Hui’s triangle:

Yang Hui's triangle is a mathematical figure made up of numbers arranged into a triangle-like shape. Each value is equal to the sum of the two values ​​above it. The shape of this triangle can be represented by a two-dimensional table, where the value at each position is calculated from the value in the previous row. In this triangle, the first row has only one value of 1, the second row has two values ​​of 1, the third row has three values ​​of 1, and so on. Starting from the fourth line, except for the 1 at the beginning and end, the middle value is the sum of the two values ​​at the corresponding position in the previous line. Here are some common features and applications of the Yang Hui triangle:

  • Symmetry: Yang Hui's triangle takes the central axis as the symmetry axis, and the values ​​at the symmetrical positions of each row are equal.
  • Properties of combination numbers: The values ​​in Yang Hui's triangle can be expressed as combination numbers. For example, the k-th value in the n-th row is expressed as C(n-1, k-1), that is, k-1 objects are selected from n-1 objects. number of combinations.
  • Power-sum property: The sum of the values ​​in each row of Yang Hui's triangle is a power of 2. For example, the sum of the values ​​in the nth row is 2^(n-1).
  • Properties of integer sequences: Each row of Yang Hui's triangle corresponds to an integer sequence, such as Fibonacci sequence, natural number sequence, etc.

Yang Hui's triangle is not only an interesting mathematical figure, but also has many practical applications. It has important applications in combinatorial mathematics, probability theory, algebra and other fields, such as calculating the expansion coefficients of binomials, solving probability distribution problems, generating polynomial coefficients, etc.
Through programming language (such as Python), Yang Hui's triangle can be realized and displayed in a visual way. Such a program can calculate and output the values ​​of Yang Hui's triangle line by line, thereby better demonstrating its laws and characteristics, and can be used for related calculations and problem solving.
Insert image description here

2. Method 1: Iteration

Code sample:

def triangle_1(x):
 """
 :param x: 需要生成的杨辉三角行数
 :return:
 """
 triangle = [[1], [1, 1]] # 初始化杨辉三角
 n = 3 # 从第三行开始计数,逐行添加
 while n <= x:
  for i in range(0, n-1):
   if i == 0:
    # 添加初始列表[1,1],杨辉三角每行的首位和末位必为1
    triangle.append([1, 1])
   else:
    # 逐位计算,并插入初始列表中
    triangle[n-1].insert(i, triangle[n - 2][i] + triangle[n - 2][i - 1])
  n += 1
 return triangle
x = 11
triangle = triangle_1(x)
  
# 遍历结果,逐行打印
for i in range(x):
 print(' '.join(str(triangle[i])).center(100)) # 转为str,居中显示

operation result:
Insert image description here

3. Method 2: Generator

Code sample:

def triangle_2(n):
 """
 :param n: 需要生成的杨辉三角行数
 :return: 
 """
 triangle = [1] # 初始化杨辉三角
 for i in range(n):
  yield triangle
  triangle.append(0) # 在最后一位加个0,用于计算下一行
  triangle = [triangle[i] + triangle[i - 1] for i in range(len(triangle))]
# 从生成器取值
for i in triangle_2(10):
 print(''.join(str(i)).center(100)) # 格式化输出

operation result:
Insert image description here

4. Method 3: Recursion

Yang Hui triangle characteristics:

[1,1]=[0,1]+[1,0]
[1,2,1]=[0,1,1]+[1, 1, 0]
[1, 3, 3, 1] = [0, 1, 2, 1] + [1, 2, 1, 0]
[1, 4, 6, 4, 1] = [0, 1, 3, 3, 1] + [1, 3, 3, 1, 0]
The nth row is equal to the Lines n-1 are padded with 0s from beginning to end, and then added bitwise

Example code:

def triangle_3(n):
 """
 :param n:需要生成的杨辉三角行数
 :return:
 """
 triangle = [1] # 初始化杨辉三角
 if n == 0:
  return triangle
 return [x+y for x, y in zip([0] + triangle_4(n - 1), triangle_4(n - 1) + [0])]
for i in range(10):
 print(''.join(str(triangle_4(i))).center(100))

operation result:
Insert image description here

This concludes this article about code examples of three methods to implement Yang Hui's triangle in python. If this article is helpful to you, please remember to like, favorite and follow~

Guess you like

Origin blog.csdn.net/beixige/article/details/133435841