PHP implementation Yang Hui triangle example

Source: Internet
Author: User

example, PHP implementation of the Yang Hui Triangle code.

    1. error_reporting (0);

    2. $iLine = 5;

    3. Function of output Yang Hui triangle

    4. function Yanghui ($iLine)
    5. {
    6. for ($i = 0; $i <= $iLine; $i + +)//Line
    7. {
    8. for ($j = 0; $j <= $i; $j + +)//Column
    9. {
    10. if ($i = = $j)//row = column (i.e. last column) or first row and first column
    11. {
    12. $a [$i] [$j] = 1;
    13. echo $a [$i] [$j]. "
      ";
    14. }
    15. else if ($i! = 0 && $j = = 0)//row = column (i.e. last column) or 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];//row + Column value = Add 2 values to the previous row
    23. echo $a [$i] [$j]. " ";
    24. }
    25. }
    26. }
    27. return $a;
    28. }

    29. Call example, print Yang Hui triangle

    30. Yanghui (5);
    31. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.