Home > Backend Development > PHP Tutorial > Example of implementing Yang Hui's triangle using PHP

Example of implementing Yang Hui's triangle using PHP

WBOY
Release: 2016-07-25 09:12:54
Original
1047 people have browsed it

Example, PHP code to implement Yang Hui's triangle.

  1. error_reporting(0);

  2. // $iLine=5;

  3. //Output Yang Hui triangle

  4. function YangHui($iLine )
  5. {
  6. for ($i = 0;$i <= $iLine;$i++)//row
  7. {
  8. for ($j = 0;$j <= $i;$j++)//column
  9. {
  10. if ($i == $j)//row = column (that is, the last column) or the first row and the first column
  11. {
  12. $a[$i][$j] = 1;
  13. echo $a [$i][$j]."
    ";
  14. }
  15. else if ($i != 0 && $j == 0)//row=column (that is, the last column) or the first row and First column
  16. {
  17. $a[$i][$j] = 1;
  18. echo $a[$i][$j]." ";
  19. }
  20. else
  21. {
  22. $a[$i ][$j] = $a[$i-1][$j]+$a[$i-1][$j-1];//The value of the row + column = the sum of the 2 values ​​​​of the previous row
  23. echo $a[$i][$j]." ";
  24. }
  25. }
  26. }
  27. // return $a;
  28. }
  29. YangHui(5);
  30. ?>

Copy code


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