JS algorithm set (ii) Fibonacci sequence
★
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 to deepen our understanding of the cycle.
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>
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 implements Fibonacci sequence (rabbit sequence)