JS algorithm Set (ii) JavaScript implementation Fibonacci sequence (rabbit sequence) JavaScript implementation Yang Hui triangle

Source: Internet
Author: User

JS algorithm set (ii) Fibonacci sequence, Yang Hui triangle

Last time I shared with you the idea of how to do the number of daffodils, and extended it to the algorithm of the self-power number, this time, we will study the Fibonacci sequence and the Yang Hui triangle to deepen our understanding of JavaScript.

First,JavaScript implements the Fibonacci sequence① to use JavaScript to implement the Fibonacci sequence, we first need to understand what the Fibonacci sequence is, the Fibonacci sequence (Fibonacci sequence), also known as the Golden Section, the mathematician Leonardo's Fibonacci (Leonardoda    Fibonacci) is introduced as an example of rabbit reproduction, which is also known as the "Rabbit series", referring to such a series: 1, 1, 2, 3, 5, 8, 13, 21, 、...... ② learned what Fibonacci numbers are and we're going to start analyzing how to do it. Through the study of law, the law, such as: ③ get the law, we have to study how to use code to achieve, specifically how to implement see Code, and comments:

    

1<script>2         3         varA = 1;//Declare the first number is 14         varb = 1;//declares a second number of 15         varC//declare an intermediate variable C6document.write (A + "<br>" +b+ "<br>");//print top two known quantity7          for(vari = 3; I <= 10; i++) {//The first two are known, starting from the third cycle, 10 is the number of printed sequence content, can be self-testing8c = a + B;//each number equals two of its front and9document.write (c+ "<br>");//print number I, take i=3; for example: c=2TenA = b;//when the next number is printed, A and B are changed, and the value of B gives the value of A,a=1,c to b,b=2; Oneb = C;//A and B are pushed backwards to perform the next operation . A         } -          -</script>

Fibonacci sequence is actually the right hand exchange of things complicated, as long as mastered the idea, all the algorithm is just that.

   second, JavaScript implementation Yang Hui triangle① to use JavaScript to achieve the Yang Hui Triangle, we first want to understand what is the Yang Hui triangle, Yang Hui Triangle, is a two-term coefficient in the triangle of a geometric arrangement. The details are as follows:
1121 131 2 141 3 3 151 4 6) 4 161 5 10 10 5 171 6 15 20 15 6 181 7 21 35 35 21 7 191 8 28 56 70 56 28 8 1Ten1 9 36 84 126 126 84 36 9 1 One1 10 45 120 210 252 210 120 45 10 1 A1 11 55 165 330 462 462 330 165 55 11 1 -1 12 66 220 495 792 924 792 495 220 66 12 1 -1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 the1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 -1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 -1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 -......

② understand what is the Yang Hui Triangle, we will begin to analyze how to do it. Below we intercept part of the Yang Hui Triangle for Analysis:

   

1           12         1  1             ① The first and last number of each row is "13       1 2  1 ② The  first row has one number, the second row has two numbers, the third row has three numbers ... 4     1  3  3  15    1  4  6  4  1③ take the Bank (line fifth) For example: the second of line fifth is equal to the sum of fourth rows 1th and second numbers, The third of line five equals the sum of the second and third numbers in line fourth,      6  ...

③ The third rule is: When the first rule is not met, the number of J in line I is equal to the sum of the number of subsections (j-1) and J of Line I-1.


④ law has been sorted out, we have to study how to use code to achieve, here we generalship with a two-dimensional array to implement, the implementation of the way to see the code, and comments:

     
1<script>2         vararr =NewArray ();//define an array3          for(vari = 0; I < 10; i++) {//make an array of length 104Arr[i] =NewArray ();//To define an array as a two-dimensional array5              for(varj = 1; j< (10-i); J + +) {//printing spaces looks good6document.write ("<span style= ' display:inline-block;width:15px; ></span> ");7             };8              for(j = 0; J < i; J + +)if(j = = 0 | | j = i-1) {//the first and I of each line equals 19ARR[I][J] = 1;//the first and last of each line is 1Tendocument.write ("<span style= ' display:inline-block;width:30px;text-align:center; > "+ arr[i][j" + "</span>"); One}Else { AARR[I][J] = Arr[i-1][j-1] + arr[i-1][j];//the number of J in line I is equal to the number of subsections (j-1) and J of Line I-1 and -document.write ("<span style= ' display:inline-block;width:30px;text-align:center; > "+ arr[i][j" + "</span>"); -             } thedocument.write ("<br>") -         } -  -</script>

Here is the execution:

The above is to share with you the JavaScript implementation of the Yang Hui triangle method, of course you can print more rows, or define the variable to accept the prompt (); method value, to manually control the number of lines printed.

Three, the following main share I do algorithm problem thinking

1) Generally get the problem, first of all to see what it is a thing

2) Second, analyze the rules inside

3) Think about how to use code to write out the rules,

4) Practice with ideas, write code in the compiler

5) encountered a bug, or card in a place with Goole console plus breakpoints for debugging, of course, you can use other browser console;

6) You will find that an algorithm is completed in one step.

7 Here is the most important one, not one, is often done an algorithm, to be good at self-summary, what problems encountered, how to solve, what kind of method can solve what problems, there is no other way to solve, there is no place careless, especially small mistakes, must pay attention to form good habits, Because practice tells us, the smaller the error hidden deeper, the less easy to find. Sometimes debugging a half-day bug found to be because of misspelled words, you will laugh and cry, even want to spank themselves. In short, the summary is the most progressive time.

Finally, I hope we can have some gains, thank you!

   


this time Here's the share .

Thank you for watching.

feel good Please praise

I hope we can inspire you .

There are better ways or different opinions please contact me in the message area .

  

JS algorithm Set (ii) JavaScript implementation Fibonacci sequence (rabbit sequence) JavaScript implementation Yang Hui triangle

Related Article

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.