The 5 characteristics of Yang Hui's triangle that are better than a cowhide!

I. Introduction

杨辉三角的历史

Yang Hui's triangle is based on the book "Detailed Nine Chapters Algorithm" written by Yang Hui in 1261. There is a picture in it, which introduces this algorithm from the book "Release Lock Calculation Book" written by another mathematician Jia Xian, but this This book has long been lost and there is no way to verify it. But what is certain is that this figure was discovered in our country no later than around 1200. In Europe, this figure is called "Pascal's triangle". Because it is generally believed that it was invented by Pascal in 1654. In fact, many people had popularized it before Pascal. The earliest was the German Pertrus APianus, who once engraved this figure on the cover of an arithmetic book written in 1527. But in any case, the discovery of Yang Hui's triangle was at least 300 years earlier in my country than in Europe.

In addition, the original name of Yang Hui's triangle is not a triangle, but the origin of the method of formulating . Later, some people called it the multiplication diagram . Because these names are too ancient, they were later shortened to "triangle". These mathematics are really very important. Every time they are mapped into the program, it is a manifestation of optimizing the for loop into an algorithm to improve execution efficiency.

2. Yanghui triangle structure

Before we start to share the characteristics and code implementation of Yang Hui's triangle, let's first understand the structure of Yang Hui's triangle.

The structure and law of Yang Hui's triangle are very simple. Except for the 1 on both sides each time, the number in the middle is the sum of the two numbers above. The triangular area shown in the figure. But it is such a simple structure, but it has a lot of mathematical logic. Including the binomial we calculated, the number of N to choose X, and the Fibonacci sequence, etc., can all be reflected in Yang Hui's triangle. Let's take a look at these features next.

3. Characteristics of Yang Hui triangle

In order to facilitate the study of the mathematical logic characteristics of Yang Hui's triangle, we arrange it in a left-aligned manner.

[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]
复制代码

Next, we will use this group of Yang Hui triangular numbers to demonstrate its mathematical logic characteristics. The Java code about Yang Hui's triangle has been placed below, and readers can refer to it.

1. Binomial expansion

Everyone must have learned the binomial expansion in school, for example: (x+y)^2 = x^2 + 2xy + y^2In fact, the mathematical logic of this expansion can be very well displayed in Yang Hui's triangle.

  • Any digital product after binomial expansion can be mapped to the corresponding number in Yang Hui's triangle.
  • The binomial expansion formula is a formula used to calculate the expansion of a given binomial to an exponential power. For a given binomial (x + y)n, the binomial expansion formula is: (x + y)^n = x^n + nx^{n-1}y + n(n-1)x^{n-2}y^2 + ... + y^nThis formula also happens to fit the numerical values ​​of Padre's triangle.

2. Number of combinations

Combination number is a mathematical concept defined in mathematics, which is used to calculate how many options there are to select several items from a set of items. For example, you have 5 kinds of fruits to eat in the morning, but you can’t eat that much. Let you choose 2 of the 5 kinds of fruits and see how many choices there are. By using the formula c(n,k) = n!/k!(nk)!, it can be calculated that there are 10 choices for 2 out of 5.

Then such a calculation can also be reflected in Yang Hui's triangle.

  • Choose 2 out of 5, you can find the 2nd column of the 5th row in the Yang Hui triangle, and the result is 10. According to this rule, 5 out of 3 = 10, 5 out of 4 = 5

3. Fibonacci sequence

The Fibonacci sequence appears in Indian mathematics and is associated with Sanskrit rhythm. In the Sanskrit poetic tradition, there is interest in enumerating all patterns in which long (L) syllables of 2-unit duration are juxtaposed with short (S) syllables of 1-unit duration.

The Fibonacci sequence can be defined by a recurrence relation:F0 = 0,F1 = 1,Fn = Fn-1 + Fn-2

F0 F1 F2 F3 F4 F5 F6 F7 F8 F9
0 1 1 2 3 5 8 13 21 34

And such a regular Fibonacci sequence is also reflected in Yang Hui's triangle.

  • Adding the diagonal numbers will result in a set of Fibonacci numbers; 1, 1, 2, 3, 5, 8, 13, 15, 33

4. Powers

There is also a very interesting feature in the Yang Hui triangle, that is, there are powers of 2 and 11 powers.

Power of 2

- The sum of the numbers in each row of Yang Hui's triangle, exactly 2 to the 0th power, 1st power...n power

11th power

  • The other is a power of 11. For example, the result of 11 to the power of 2 is exactly the combination of the row of numbers 121. If it is the 5th power of 11, and there are consecutive 10s in the middle, it is to carry the next digit to the previous one.

5. Square numbers

  • There is also a law of square numbers in Yang Hui's triangle. For example, the square of 3 is exactly the result of 3+6 on the right. The square of 4 is the result of 6+10 on the right.

4. Realization of Yang Hui Triangle

Next, we realize the Yang Hui triangle;

public HashMap<Integer, Integer> pascalTriangle(int lineNumber) {
    HashMap<Integer, Integer> currentLine = new HashMap<>();
    currentLine.put(0, 1);
    int currentLineSize = lineNumber + 1;
    for (int numberIdx = 1; numberIdx < currentLineSize; numberIdx += 1) {
        /*
         * https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/math/pascal-triangle/pascalTriangle.js
         * 第i行号中的第 -th 个条目lineNumber是 Binomial CoefficientC(lineNumber, i)并且所有行都以 value 开头1。这个思路是C(lineNumber, i)使用C(lineNumber, i-1). 它可以O(1)使用以下方法及时计算:
         * C(lineNumber, i)   = lineNumber! / ((lineNumber - i)! * i!)
         * C(lineNumber, i - 1) = lineNumber! / ((lineNumber - i + 1)! * (i - 1)!)
         *
         * 从以上两个表达式我们可以推导出下面的表达式:C(lineNumber, i) = C(lineNumber, i - 1) * (lineNumber - i + 1) / i
         * 所以C(lineNumber, i)可以从C(lineNumber, i - 1)时间上算出来O(1)
         */
        currentLine.put(numberIdx, ((null == currentLine.get(numberIdx - 1) ? 0 : currentLine.get(numberIdx - 1)) * (lineNumber - numberIdx + 1)) / numberIdx);
    }
    return currentLine;
}
复制代码

unit test

@Test
public void test_PascalTriangle() {
    PascalTriangle pascalTriangle = new PascalTriangle();
    for (int i = 0; i <= 10; i++) {
        HashMap<Integer, Integer> currentLineMap = pascalTriangle.pascalTriangle(i);
        System.out.println(JSON.toJSONString(currentLineMap.values()));
    }
}

[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]
复制代码
  • In this way, we can get a set of Yang Hui triangular sequence.

5. Common interview questions

  • What are the uses of the Yang Hui triangle?
  • Realize the lower Yang Hui triangle with code. —— There are also such questions in LeetCode

Guess you like

Origin blog.csdn.net/Java_LingFeng/article/details/128685349