Use java to implement the sample code of the Yang Hui triangle,

Source: Internet
Author: User

Use java to implement the sample code of the Yang Hui triangle,

A student asked me a java interview question before. The question is not difficult. Use java to implement the Yang Hui triangle. I spent some time sorting out and found it interesting. So I want to write it down and share it. Before writing code, we should clarify the following two issues.

What is Yang Hui triangle?

The Yang Hui triangle is a geometric arrangement of the binary coefficient in the triangle. It was mentioned in the chapter 9 algorithm written by Yang Hui, a mathematician in the Southern Song Dynasty in 1261. It is called the Pascal triangle in Europe ,.

Yang Hui triangle

The law of the Yang Hui triangle is the principle

1. Each number is equal to the sum of the two numbers above it.

2. The numbers in each row are symmetric, and gradually increase from 1.

3. There are n numbers in the nth row.

4. The number in line n is 2n-1.

5. The number of m in the nth row can be expressed as C (n-1, m-1), that is, the number of combinations of m elements from n-1 different elements.

6. The number of m in row n is equal to the number of n-m + 1, which is one of the combinations.

7. Each number is equal to the sum of the left and right numbers in the previous row. The entire Yang Hui triangle can be written in this way. That is, the number of I in the n + 1 row is equal to the sum of the number of I-1 in the n row and the number of I, which is also one of the properties of the combination number. C (n + 1, I) = C (n, I) + C (n, I-1 ).

8. The coefficients in the expansion of (a + B) n correspond to each item in the (n + 1) row of the Yang Hui triangle in sequence.

9. Set the number of rows 2n + 1 to 1st, and the number of rows 2n + 2 to 3rd, and the number of rows 2n + 3 to 5th ...... Connect them to the first line. The sum of these numbers is the number of the 4n + 1 Fibonacci; the number of the 2n row is 2nd (n> 1 ), the number of rows 2n-1 and the number of rows 2n-2 ...... The sum of these numbers is the 4n-2 Fibonacci number.

10. Sort the numbers of all rows to the power of N-1 (n is the number of rows) of 11: 1 = 11 ^ 0; 11 = 11 ^ 1; 121 = 11 ^ 2 ...... If n> 5 does not meet this condition, the rightmost number "1" in the nth row should be placed in a single position, then align the single digit on the left to ten digits ......, similarly, fill in the vacant space with "0" and add all the numbers to the power of N-1 of 11. Taking n = 11 as an example, the number of 11th rows is: 45,120,210,252,210,120, 25937424601, 1110, 1, and the result is =.

After we have understood these two points, our thinking is very clear. There are many implementation methods. Here I plan to use a two-dimensional array and a dual for loop.

Demo code:

Public class Yanghui {public static void main (String [] args) {// create a two-dimensional array int t [] [] = new int [10] []; // traverse the first layer of the Two-dimensional array for (int I = 0; I <t. length; I ++) {// initialize the size of the second layer group t [I] = new int [I + 1]; // traverse the second layer group for (int j = 0; j <= I; j ++) {// assign the array elements on both sides to 1 if (I = 0 | j = 0 | j = I) {t [I] [j] = 1 ;} else {// other numerical value t [I] [j] = t [I-1] [j] + t [I-1] [J-1];} // output array element System. out. print (t [I] [j] + "\ t");} // wrap the System. out. println ();}}}

The output result on the console is as follows:

Here, only the Yang Hui triangle of ten rows is output. After optimization, you can change it to dynamic row acquisition. It can also be changed to a positive triangle. You only need to add a loop to calculate spaces. If you are interested, try it. --- From java

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.