Example of the javascript Yang Hui triangle

Source: Internet
Author: User
Example of the javascript Yang Hui triangle

The Yang Hui triangle is an indispensable tool for calculating the coefficients of the two-digit multiplication and square expansion. it is a triangle number table composed of numbers.

The effect is as follows:

Data: Yang Hui triangle n rows of 1st number is 1, the second number is 1 x (n-1), the third number is 1 x (n-1) x (n-2)/2, the fourth number is 1 × (n-1) × (n-2)/2 × (n-3)/3... And so on. Another important feature of the Yang Hui triangle is that the numbers at the beginning and end of each line are 1. the numbers in the middle are equal to the sum of the two adjacent numbers in the previous line, which is usually used in the arrangement and combination: C (m, n) = C (m-1, n-1) + C (m-1, n) based on the above nature, you can use the function to easily calculate Yang Hui's triangle. the Function accepts a parameter, that is, the number of rows in Yang Hui's triangle to be obtained:

  1. Function Pascal (n) {// Yang Hui triangle, where N is the number of rows
  2. //
  3. }

In this function, two for loops are nested, the number of outer loops is the number of rows, and the inner loop is each of the rows:

  1. For (var I = 0; I <n; I ++) {// N rows in total
  2. For (var j = 0; j <= I; j ++) {// The number of numbers in each row is the row number, for example, the number of rows 1 and the number of rows 2
  3. }
  4. Document. write ("
    ");
  5. }

In each row, each number is a combination of C (m, n), where m is the row number (from 0), n is the serial number in the row (from 0), that is:

  1. Document. write (Combination (I, j) + ""); // The content in the quotation marks is two html spaces () characters.

Combination (I, j) is a function used to calculate the number of combinations. this function adopts the characteristics of composite numbers C (m, n) = C (S-1, n-1) + C (S-1, n) the most effective method for such features is recursion:

  1. Function Combination (m, n ){
  2. If (n = 0) return 1; // The first number of each row is 1
  3. Else if (m = n) return 1; // The last number is 1
  4. // The rest are added together.
  5. Else return Combination (m-1, n-1) + Combination (m-1, n );
  6. }

Js prints the complete code of the Yang Hui triangle:

  1. <% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
  2. <%
  3. String path = request. getContextPath ();
  4. String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
  5. %>
  6. <% --
  7. Yang Hui triangle
  8. CreateDate 2010-7-8
  9. Author is new to learning
  10. -- %>
  11. Yang Hui triangle --bbs.it-home.org

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.