Algorithm exercise four: print Yang Hui triangle

Source: Internet
Author: User

Rule: 1. The sum of the nth layer equals the n power of 2; 2. Starting from the third level the number equals the sum of the two numbers above it; 3. The length of each line equals n, and the loop n times;
            functiontext (n) {vararr = [];//Results                varARR1 = [];//Relay                //generate Yang Hui triangle data                 for(vari = 1; I <= N; i++) {arr1= [1];//The first value is "1"                     for(varj = 1; J < I; J + +){                        if(j = = I-1) {Arr1[j]= 1;//the last value is "1"}Else{Arr1[j]= Arr[i-2][j] + arr[i-2][j-1];//The median value is the number of two values above it and}} arr.push (ARR1); }                   //iterate through arrays, print data                 for(varK = 0;k < arr.length; k++){                    vari = "; varARR2 = [];  for(varL = 0; L < arr.length-k;l++) {Arr2.push (i); } console.log (Arr2.join (") + arr[k].join (' ') }}} text (10);

Algorithm exercise four: print Yang Hui triangle

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.