帕斯卡三角形公式及其示例

⚡ 智能摘要

Pascal's Triangle is a triangular arrangement of numbers where each value equals the sum of the two numbers directly above it, revealing deep patterns in combinatorics, binomial expansions, and probability that have fascinated mathematicians for centuries.

  • 🔺 结构体: 每行以 1 开头和结尾,内部数值由上面两个数字相加得出。
  • 📐 二项式链接: 第 n 行第 k 列等于二项式系数 C(n, k),使得该三角形成为组合的视觉查找工具。
  • 🔢 隐藏的模式: 行和等于 2 的幂,对角线和生成斐波那契数列。
  • ✅ 三种方法: 你可以通过前面的行来构建它,通过计算二项式系数,或者通过迭代修正系数快捷方式来构建它。
  • 🧪 应用环境: 在现代课程中,代数、概率论、计算机科学和组合证明等学科都有应用。

什么是帕斯卡三角形?

Pascal's Triangle is a triangular array of numbers that follows a simple pattern based on the row above it. It was popularized by French mathematician Blaise Pascal in the 17th century. The triangle begins with a single "1" at the top, and every subsequent row starts and ends with "1" as well.

帕斯卡三角形

Beyond its elegant shape, Pascal's Triangle encodes deep mathematical relationships. It is closely tied to the binomial theorem, combinatorial counting, and probability, which is why it appears in algebra, statistics, and computer science classrooms worldwide.

帕斯卡三角形的历史

Although named after Blaise Pascal, the triangle predates him by centuries. The Chinese mathematical text "The Nine Chapters on the Mathematical Art" contains one of the earliest known examples, displaying many of the same patterns we use today.

波斯数学家卡拉吉和印度学者 Pingala also explored similar arrays. Pascal formalized the triangle's properties in his 1654 treatise "Traité du triangle arithmétique," which gave the structure its modern name in Western mathematics.

帕斯卡三角形的构造

Constructing Pascal's Triangle is straightforward. The only rule to remember is that each row starts and ends with 1, and every other number is built from the row above.

对于任意行 r 和列 c,其值等于行 r-1 的列 c-1 和列 c 中的数字之和。

在这里,

  • r = 3, 4, 5, ...
  • n and c = 2, 3, 4, ..., r-1.

Here are the steps to build Pascal's Triangle:

步骤1) 先填写前两行。

帕斯卡三角形的构造

步骤2) 第三行的第二个元素是第二行第一个数字和第二个数字之和。

帕斯卡三角形的构造

步骤3) The fourth row begins with "1". The second number is 3, which is the sum of 1 and 2 (highlighted in blue).

下图展示了如何填写第四行:

帕斯卡三角形的构造

步骤4) 第五行由五个数字组成。我们已经从前面的步骤中知道了填充行的规律。

帕斯卡三角形的构造

帕斯卡三角公式 - 二项式系数

A binomial coefficient counts the number of ways to choose a subset of k elements from a collection of n elements. It is commonly written as "C(n, k)" or "n choose k."

二项式系数定义为:

帕斯卡三角公式 - 二项式系数

The "!" symbol denotes the factorial of a number.

n! = n.(n-1).(n-2)...3.2.1

例如,

5! = 5.4.3.2.1

= 120

So, C(5, 3) or "5 choose 3" = 5! / 3!(5-3)!

= 120 / 12

= 10

方法一:通过前一行构建帕斯卡三角形

The procedure here mirrors how we drew the triangle manually. Suppose we want to generate Pascal's triangle up to seven rows.

具体步骤如下:

步骤1) Start the topmost row with "1".

步骤2) For row "r", element "c" will be the sum of column "c-1" and column "c" of row "r-1".

步骤3) The first and last numbers in every row will always be "1".

遵循这三个简单的步骤,我们就可以系统地构建整个三角形。

C++ Code 上一行帕斯卡三角形

#include <bits/stdc++.h>
using namespace std;
void printRow(int n)
{
  int numbers[n][n];
  for (int row = 0; row < n; row++)
  {
    for (int col = 0; col <= row; col++)
    {
      if (col == 0 || col == row)
      {
        numbers[row][col] = 1;
      }
      else
      {
        numbers[row][col] = numbers[row - 1][col - 1] + numbers[row - 1][col];
      }
      cout << numbers[row][col] << "\t";
    }
    cout << endl;
  }
}
int main()
{
  int n;
  cout << "How many rows: ";
  cin >> n;
  printRow(n);
}

输出:

How many rows: 7
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

Python Code 上一行帕斯卡三角形公式

def printRow(n):
    numbers = [[0 for row in range(n)]
               for col in range(n)
               ]
    for row in range(len(numbers)):
        for col in range(0, row+1):
            if row == col or col == 0:
                numbers[row][col] = 1
            else:
                numbers[row][col] = numbers[row-1][col-1]+numbers[row-1][col]

            print(numbers[row][col],end="\t")
        print("\n")
n = int(input("How many rows: "))
printRow(n)

Pascal's Triangle Example Output:

How many rows: 7
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

复杂度分析

A 二维数组 is used in this implementation. Given that N is the number of rows in Pascal's triangle, this requires N2 单位空间。因此,空间复杂度为 O(N)。2).

The function uses two nested loops, each running up to "N" times. So, the time complexity is also 在2)或时间复杂度的平方。

方法二:通过计算二项式系数构建帕斯卡三角形

We can derive the numbers of Pascal's triangle directly using binomial coefficients. The diagram below illustrates the relationship:

利用二项式系数构建帕斯卡三角形

Here are the steps to build Pascal's Triangle by calculating the binomial coefficient:

步骤1) 最上面一行是 C(0, 0)。根据上面的公式,C(0, 0) = 1,因为 0! = 1。

步骤2) For row "i", there will be a total of "i" elements. Each item is calculated as C(n, r), where n is i-1.

步骤3) Repeat step 2 for as many rows of Pascal's triangle as you want to generate.

C++ Code 帕斯卡三角形的二项式系数

#include <iostream>
using namespace std;
int factorial(int n)
{
    int result = 1;
    for (int i = 1; i <= n; i++)
    {
        result *= i;
    }
    return result;
}
int binomialCoefficient(int n, int r)
{
    int result = 1;
    if (r > n)
    {
        return -1;
    }
    result = factorial(n) / (factorial(r) * factorial(n - r));
    return result;
}
void printPascalTriangle(int row)
{
    for (int i = 0; i <= row; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout << binomialCoefficient(i, j) << "\t";
        }
        cout << endl;
    }
}
int main()
{
    int n;
    cout << "Enter row number: ";
    cin >> n;
    printPascalTriangle(n);
}

输出:

Enter row number: 9
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

Python Code 帕斯卡三角形的二项式系数

def factorial(n):
    result = 1
    for i in range(1,n+1):
        result*=i
    return result
def binomialCoefficient(n,r):
    result =1
    if r>n:
        return None
    result = factorial(n) / (factorial(r) * factorial(n - r))
    return int(result)
def printPascalTriangle(row):
    for i in range(row+1):
        for j in range(i+1):
            print(binomialCoefficient(i, j), end="\t")
        print()
# print(binomialCoefficient(3, 2))
n = int(input("Enter row number: "))
printPascalTriangle(n)

Pascal's Triangle Example Output:

Enter row number: 8
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

复杂度分析

Three loops are used in this implementation: one to compute the binomial coefficient and two more to iterate through every row and column. With respect to the number of rows, all three loops run up to "n" times. Consequently, the overall time complexity is O(n3).

由于我们不存储任何中间结果,因此空间复杂度为常数。程序实时计算每个元素并将其打印在一行中,因此空间复杂度降低为 O(1).

方法 3:利用修正二项式系数构建帕斯卡三角形

在之前的方法中,我们使用二项式系数公式来计算每个元素。改进后的方法直接从 C(n, r-1) 推导出 C(n, r),从而减少了一个数量级的计算量。

Here are the steps to build Pascal's Triangle by the modified binomial coefficient:

步骤1) Initiate the first row with "1".

步骤2) Calculate C(n, r), where "n" is the row number and "r" is the column index. Assign that value to a variable C.

步骤3) For computing the next coefficient, use C * (n - k) / k. Assign this new value back to C.

步骤4) Continue step 3 until "k" reaches the end of the row. After each iteration, increment k by one.

C++ Code 利用修正二项式系数求解帕斯卡三角形

#include <bits/stdc++.h>
using namespace std;
void printpascalTriangle(int n)
{
  for (int row = 1; row <= n; row++)
  {
    int previous_coef = 1;
    for (int col = 1; col <= row; col++)
    {
      cout << previous_coef << "\t";
      previous_coef = previous_coef * (row - col) / col;
    }
    cout << endl;
  }
}

int main()
{
  int n;
  cout << "How many rows: ";
  cin >> n;
  printpascalTriangle(n);
}

输出:

How many rows: 5
1
1       1
1       2       1
1       3       3       1
1       4       6       4       1

Python Code 利用修正二项式系数求解帕斯卡三角形

def printpascalTriangle(n):
    for row in range(1, n+1):
        previous_coef = 1
        for col in range(1, row+1):
            print(previous_coef, end="\t")
            previous_coef = int(previous_coef*(row-col)/col)
        print()
n = int(input("How many rows: "))
printpascalTriangle(n)

Pascal's Triangle Patterns Output:

How many rows: 5
1
1       1
1       2       1
1       3       3       1
1       4       6       4       1

复杂度分析

The implementation uses two loops, each running a maximum of "n" times, where "n" is the number of rows in the triangle. So, the time complexity is 上2),平方时间。

就空间复杂度而言,我们不需要任何数组来存储。我们只使用一个变量来保存之前的二项式系数,因此只需要一个额外的空间。因此,空间复杂度为 O(1).

帕斯卡三角形的应用

Here are some practical applications of Pascal's Triangle:

二项式展开式: The coefficients of any binomial expansion can be read directly from Pascal's triangle. Here is an example:

(x + y)0 1
(x + y)1 1.x + 1.y
(x + y)2 1x2 + 2坐标 + 1y2
(x + y)3 1x3 + 3x2和+ 3xy2 + 1y3
(x + y)4 1x4 + 4x3和+ 6x2y2 + 4xy3 + 1y4

计算组合: The elements of Pascal's triangle correspond directly to binomial coefficients. For example, if you have 6 balls and want to choose 3, the answer is 6C3. You can find that value in the 3rd element of the 6th row of Pascal's triangle.

可能性: Pascal's Triangle is widely used to compute probabilities in coin tosses, dice problems, and other combinatorial events where each outcome corresponds to a binomial distribution.

关于帕斯卡三角形的有趣事实

Here are some facts you will find interesting about Pascal's triangle:

  • 任意一行中所有元素的总和始终是 2 的幂。

关于帕斯卡三角形的事实

  • 各行的对角线和构成斐波那契数列。

关于帕斯卡三角形的事实

  • 每一行对应于 (a+b) 展开式中的系数n.
  • 如果只给奇数部分涂上阴影,得到的图形就是谢尔宾斯基三角形分形。

常见问题

虽然这个三角形是以布莱兹·帕斯卡的名字命名的,他于1654年正式提出了这个三角形,但早在几个世纪前,中国、印度和波斯就已经知道这个三角形了。贾宪、杨辉等数学家也曾研究过这个三角形。 Pingala,而且早在帕斯卡之前,Al-Karaji 就研究过类似的阵列。

帕斯卡三角形中的每个元素都等于二项式系数 C(n, k)。第 n 行的数字给出了 (a + b) 的 n 次方的系数,这使得该三角形成为二项式展开的快速查找表。

帕斯卡三角形第 n 行所有数字之和等于 2 的 n 次方。例如,第 4 行包含 1、4、6、4、1,它们的和为 16,正好是 2 的 4 次方。

如果将帕斯卡三角形浅对角线上的数字相加,得到的和正好是斐波那契数列:1、1、2、3、5、8、13,以此类推。这是三角形最精妙的隐藏图案之一。

帕斯卡三角形用于模拟两种等可能结果的事件的概率,例如抛硬币。第 n 行表示 n 次抛硬币得到 k 次正面的方法数,这直接关系到二项概率分布。

人工智能系统利用帕斯卡三角形中的二项式系数进行特征选择、采样和组合优化。强化学习智能体和符号数学求解器在推理多项式展开和离散选择问题时也会参考帕斯卡三角形。

是的。人工智能数学辅导工具可以生成逐行可视化图表、自适应练习题,并针对二项式系数练习提供即时反馈。它们可以帮助学习者按照自己的节奏,将三角形与组合、概率和二项式定理联系起来。

总结一下这篇文章: