Simple example of php Yang Hui triangle

Source: Internet
Author: User
Simple example of php Yang Hui triangle

For example, according to the Yang Hui triangle, "each number is equal to the sum of the two numbers in the previous line ".

  1. // Error message

  2. Error_reporting (e_all &~ E_deprecated &~ E_notice );
  3. // Print the functions of the Yang Hui triangle
  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 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 sum of the two values in the previous row
  19. }
  20. } // Bbs.it-home.org
  21. }
  22. Return $;
  23. }

  24. // Print the Yang Hui triangle

  25. $ Adata = yanghui (4 );
  26. Echo'
    ';  
  27. print_r($adata);
  28. echo '
  29. ';
  30. ?>

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.