How to realize Yang Hui's isosceles triangle in PHP

PHPz
Release: 2023-04-24 14:10:17
Original
660 people have browsed it

Yang Hui Isosceles triangle is one of the classic figures in mathematics. It not only has a beautiful appearance, but also has good mathematical properties and is widely used in various fields. In computer programming, we can also use various languages ​​to implement Yang Hui's isosceles triangle generation algorithm. This article will introduce how to use PHP language to implement Yang Hui's isosceles triangle generation.

  1. The concept of Yang Hui’s isosceles triangle

Yang Hui’s isosceles triangle is also called Pascal’s triangle. It is a triangle composed of numbers. Write the number 1 on the top of the triangle. In the lower row of the triangle, each number is the sum of the two numbers above it, in the row below it, and so on. Moreover, both ends of each row are 1.

The following is an example of Yang Hui's isosceles triangle:

       1
      1 1
     1 2 1
    1 3 3 1
  1 4  6  4 1
1 5 10 10 5 1
Copy after login

Among them, the kth number in the nth row can be expressed as Combination(n-1, k-1), that is, from n- Select the number of combinations of k-1 numbers from 1 number. Therefore, Yang Hui's isosceles triangle is also widely used in combinatorics.

  1. Use PHP to realize Yang Hui’s isosceles triangle

In PHP, we can use loop statements and arrays to realize the generation of Yang Hui’s isosceles triangle. The specific implementation method is as follows:

<?php
function generatePascalTriangle($numRows) {
  $triangle = [];
  if ($numRows > 0) {
    $triangle[] = [1];
    for ($i = 1; $i < $numRows; $i++) {
      $row = [$triangle[$i - 1][0]];
      for ($j = 1; $j < $i; $j++) {
        $row[] = $triangle[$i - 1][$j - 1] + $triangle[$i - 1][$j];
      }
      $row[] = $triangle[$i - 1][$i - 1];
      $triangle[] = $row;
    }
  }
  return $triangle;
}
Copy after login

In the above code, we first define a function called generatePascalTriangle, which accepts a parameter $numRows, indicating the number of rows of Yang Hui’s isosceles triangle that needs to be generated. Next, we create an array named $triangle to store the numbers in each row.

If $numRows is greater than 0, we add the number 1 in the first row to $triangle. Then, use a loop statement to traverse starting from the second line. Each traversal generates a row of numbers and adds them to $triangle. In the process of generating each row of numbers, we use another loop statement and use the numbers in the previous row to calculate the numbers in the current row. Specifically, we first use the first number of the current row, that is, $triangle[$i - 1][0], and then calculate the middle number of the current row in sequence, that is, $triangle[$i - 1][$j - 1] $triangle[$i - 1][$j], and finally add the last number of the current row, $triangle[$i - 1][$i - 1]. Finally, we get the number $row of the current row and add it to $triangle. Finally, the function returns the $triangle array and the generation is completed.

When using the above function to generate Yang Hui's isosceles triangle, we can set $numRows to any positive integer to control the number of rows to generate Yang Hui's isosceles triangle. At the same time, we can use a double loop to output the generated numbers graphically, thereby presenting the beautiful appearance of Yang Hui's isosceles triangle.

The following is an example of Yang Hui's isosceles triangle generated using the above function:

$numRows = 6;
$triangle = generatePascalTriangle($numRows);
for ($i = 0; $i < $numRows; $i++) {
  for ($j = 0; $j <= $i; $j++) {
    echo $triangle[$i][$j] . " ";
  }
  echo "<br>";
}
Copy after login

In the above code, we first set $numRows to 6, indicating that 6 rows of numbers need to be generated. Then, use the generatePascalTriangle function to generate Yang Hui's isosceles triangle, and use a double loop to traverse the $triangle array to output the numbers in a graphical form. Finally, we can get the following output result:

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1
Copy after login
  1. Summary

This article introduces the concept of Yang Hui’s isosceles triangle and its application in combinatorial mathematics. At the same time, We also introduced the use of PHP language to implement Yang Hui's isosceles triangle generation algorithm. Through the introduction of this article, readers can learn how to use arrays and loop statements to generate Yang Hui's isosceles triangle, and can also apply arrays and loop statements to solve other similar problems.

The above is the detailed content of How to realize Yang Hui's isosceles triangle in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!