laitimes

Implement Yanghui's triangle with Python programming

author:Little bees share the trivialities of life

#暑期创作大赛#

Introduction

Yang Hui triangle is a fascinating mathematical structure whose wonderful arrangement of numbers attracts the attention of mathematicians and programming enthusiasts. In this blog, we will explore how to write a simple program in Python to generate Yanghui triangles.

Meet the Yanghui Triangle:

Yanghui triangle, also known as Pascal's triangle, is a triangular pattern arranged in numbers. Each number is the sum of its top-left and upper-right numbers, with a preceded row of 1.

Implementation ideas and algorithms:

The basic idea of generating Yang Hui triangle is to start with the second row, and each number is equal to the sum of the two numbers above it. We can implement this process using nested loops.

To write a Python program:

Below is a simple Python program to generate a Yanghui triangle for a specified number of rows.

def generate_pascals_triangle(rows):
    triangle = []
    for i in range(rows):
        row = [1] * (i + 1)
        if i > 1:
            for j in range(1, i):
                row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j]
        triangle.append(row)
    return triangle

def print_pascals_triangle(triangle):
    for row in triangle:
        print(" ".join(map(str, row)).center(len(triangle[-1]) * 3))

rows = 5  # 指定要生成的行数
pascals_triangle = generate_pascals_triangle(rows)
print_pascals_triangle(pascals_triangle)           

Code examples and explanations:

The above code includes two functions, one for generating Yanghui triangle and the other for printing Yanghui triangle. The generate_pascals_triangle function uses list nesting to store the numbers for each row and two layers of loops to calculate the value of each number. The print_pascals_triangle function prints the resulting triangle and polishes the output format by centering the string.

print(" ".join(map(str, row)).center(len(triangle[-1]) * 3)) converts the numbers of the current line to strings, separated by spaces, and center-aligned by the .center() method. len(triangle[-1]) * 3 is used to calculate the length of each row for proper centering.

Custom number of lines and output format:

By modifying the value of the rows variable, you can generate Yanghui triangles with different numbers of rows. You can also adjust the output format to display in a more aesthetically pleasing way in the terminal. The result of the run is shown below.

Implement Yanghui's triangle with Python programming

We provide a simple code as shown below, the core principle is the same, but the printed Yanghui triangle is a right triangle.

def generate_pascal_triangle(n):
    triangle = []
    for i in range(n):
        row = [1 if j == 0 or j == i else row[j-1] + row[j] for j in range(i + 1)]
        triangle.append(row)
    return triangle

def print_pascal_triangle(triangle):
    for row in triangle:
        print(' '.join(map(str, row)))

num_rows = 6  # 要生成的行数
pascal_triangle = generate_pascal_triangle(num_rows)
print_pascal_triangle(pascal_triangle)           

The result of the run is shown below.

Implement Yanghui's triangle with Python programming

Apps and extensions:

Yang Hui triangle has a wide range of applications in combinatorics, probability theory and other fields. If you have time, you can try to extend this program, such as writing a function to calculate the value of a specific position, or generating a larger Yanghui triangle.

End:

With this simple Python program, we have successfully implemented the function of generating Yang Hui triangle. Programming not only deepens conceptual understanding, but also explores the beauty of mathematics in creative ways.

Implement Yanghui's triangle with Python programming

Read on