Home > Backend Development > PHP Tutorial > PHP Yang Hui triangle simple example

PHP Yang Hui triangle simple example

WBOY
Release: 2016-07-25 09:12:56
Original
1271 people have browsed it

Example, according to Yang Hui's triangle "each number is equal to the sum of the left and right numbers in the previous row".

  1. //Error message

  2. error_reporting(e_all & ~e_deprecated & ~e_notice);
  3. //Print Yang Hui triangle function
  4. function yanghui($iline)
  5. {
  6. $a = array();
  7. $nb = '';
  8. for ($i = 0;$i <= $iline;$i++)//line
  9. {
  10. for ($j = 0;$j < ;= $i;$j++)//Column
  11. {
  12. if ($i == $j || $j == 0)//Row = column (that is, the last column) or the first row and the first column
  13. {
  14. $a[$i][$j] = 1;
  15. }
  16. else
  17. {
  18. $a[$i][$j] = $a[$i-1][$j]+$a[$ i-1][$j-1];//The value of the row + column = the addition of the two values ​​​​of the previous row
  19. }
  20. } // bbs.it-home.org
  21. }
  22. return $a;
  23. } < /p>
  24. //Print Yang Hui triangle

  25. $adata = yanghui(4);
  26. echo '
    '; </li>
    <li>print_r($adata); </li>
    <li>echo '
    ';
  27. ? >

Copy code


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