-JS realization of the Fibonacci sequence formula algorithm

Source: Internet
Author: User

Before the Fibonacci sequence was counted as the sum of the first two numbers.

such as 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

2=1+1

3=1+2

5=2+3

8=3+5

......

In fact, there is another rule:

2 = 1*2-0
3 = 2*2-1
5 = 3*2-1
8 = 5*2-2
13= 8*2-3
21=13*2-5

......

The following is the JS implementation of the Code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metahttp-equiv= "X-ua-compatible"content= "Ie=edge,chrome=1"><title>JS Fibonacci sequence</title><Metaname= "description"content= "js Fibonacci sequence"><Metaname= "keywords"content= "js Fibonacci sequence"><Linkhref=""rel= "stylesheet"><Scripttype= "Text/javascript">    /*found a magic algorithm to simplify the Fibonacci sequence, handy with JS implementation.     The following key sentences are the rules 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368 You can find 2 = 1*2-0 3 = 2*2-1 5 = 3*2-1 8 = 5*2-2 13 = 8*2-3 21=13*2-5 ... You know the following.*/    vara= []; a[0]=0; a[1]=1; a[2]=1; vari;  for(i=3; I< About; I++) {A[i]= 2*A[i-1]-A[i-3];/*Key Sentence*/Console.log (A[i]); }</Script></Head><Body> </Body></HTML>

-JS realization of the Fibonacci sequence formula algorithm

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.