Home > Backend Development > PHP Problem > How to obtain a 10-line Yang Hui triangle through a PHP program

How to obtain a 10-line Yang Hui triangle through a PHP program

青灯夜游
Release: 2023-03-12 06:02:01
Original
3850 people have browsed it

Those who have studied programming languages ​​or algorithms are bound to come into contact with the Yang Hui Triangle (one of the outstanding research results of ancient Chinese mathematics). It itself is not profound. Today we will use PHP to write a program to output a Yang Hui triangle with 4 rows and 3 columns.

First of all, let’s understand Yang Hui Triangle.

Yang Hui's triangle is a geometric arrangement of Newton's binomial coefficients in a triangle. Each number in it is equal to the sum of the two numbers above.

How to obtain a 10-line Yang Hui triangle through a PHP program

We can easily find the pattern from the above picture:

  • The elements before and after each row are 1

  • There are several elements in whichever row

  • Starting from the third row, except for the beginning and end of each row, every other element is above The sum of two adjacent elements (the sum of the number at the same position in the previous row and the previous digit at the same position in the previous row)

After finding the pattern, we can set up the algorithm, Then set the program according to the algorithm (this article uses array double for loop nesting to implement Yang Hui's triangle. If you don't know about for loops, you can check out "PHP Loop Learning 3: How to use for loop statements to traverse arrays" )

Algorithm analysis: There is an array with row i and column j (j is less than or equal to i)

  • Double for loop, the first level of loop, traverses row i of the array; The second level of loop traverses column j of the array. And the column j value of the array must be less than or equal to the row i value of the array

  • Use the if statement in the second-level loop body to make a judgment. When j=1 or # When ##i=j, the value is $array[i][j] = 1;

  • ## and other times,
  • $array[i][j]

    = $array[ i-1 ][ j-1 ] $array[ i-1 ][ j ]

  • Let’s take a look at the PHP program code that implements the function:
<?php
function yh_put($max) {
    $array = [];
    // 循环列数
    for($i = 1; $i <= $max; $i++) {
        // 循环行数
        for( $j=1; $j <= $i; $j++) {
            // 每行的第一个和最后一个都是1
            if( $j == 1 || $j == $i ) {
                echo $array[$i][$j] = 1;
            } else {
                // 上一行同位置的数 + 上一行同位置的前一位数之和
                echo $array[$i][$j] = $array[ $i-1 ][ $j-1 ]+$array[ $i-1 ][ $j ];
            }
            echo "  ";
        }
        echo "<br>";
    }
    unset($array);
}
yh_put(10);
Copy after login

The output result is:


How to obtain a 10-line Yang Hui triangle through a PHP programOkay That’s all. If you want to know anything else, you can click here. → →

php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:

PHP function array array function video explanation

, come and learn!

The above is the detailed content of How to obtain a 10-line Yang Hui triangle through a PHP program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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