JavaScript implements different positions of Fibonacci sequences

Source: Internet
Author: User

Fast New Year, the company is basically gone, only the best talent in the Republic, the most important posts in each department will stick to their own swivel chair, after all, each person's ability is limited, rather than let them continue to work, rather put them home for the new year. This consciousness is very high, this understanding is very painful ~

Idle nothing to do (in fact, I have things to do, but I do not want to do, as a socialist successor, I think I have the right to be so capricious! ), thinking of a very interesting rabbit problem: In general, rabbits in the two months after birth, there is the ability to reproduce, a pair of rabbits can produce a pair of rabbits each month. If all the rabbits are not dead, how many pairs of rabbits can be bred in a year?

The first clear test instructions, is to seek the total logarithm of rabbits per month. The rabbit is divided into three kinds, the rabbit was born the first month for the rabbit, the second month for the Rabbit, the third month for the Big Rabbit (can reproduce the rabbit), so the first month of the Rabbit logarithm is: 1, 0, 0; The total logarithm of the second month is 0, 1, 0; the logarithm of the rabbit is 1, 0, The fourth month of the Rabbit logarithm is: 1, 1, 1; the logarithm of the fifth month is: 2, 1, 2, sixth month: 3, 2, 3, seventh month: 5, 3, 5, seventh months: 8, 5, 8 ... the corresponding rabbit logarithm for each month is: 1, 1, 2, 3, 5, 8, 13 ... Carefully observe the logarithm of the rabbit each month, starting from the third month, the logarithm of the rabbit every month is the logarithm of the rabbit in the previous month.  That would be a good thing to do. We use the first way to implement the queue: Create an array, record the first two months of the rabbit, while you can use the array of push (), Shift () method to simulate some of the characteristics of the queue, remember, eat more pull is the queue, eat more vomit is the stack.

1  functionFibonacci (len) {2 3            //first create a Fibonacci array, record the logarithm of the rabbit, because the logarithm of the rabbit in the first two months is OK, so we want to set Fibonacci within the first two months value. 4Const Fibonacci = [1, 1];5           6              //Copy this array for the rabbit's queue operation7Const ARR =Array.from (Fibonacci);8         9            //Loop calculates the total logarithm of the rabbit of the first Len monthTen              for(Let i = 0; i < len-2; i++) { One  A               //Fibonacci each time two months before adding the logarithm of the rabbit and -Fibonacci.push (Arr.reduce (sum, value) ={ -                     returnSum +value; the}, 0)); -  -                //The total number of rabbits in the current month into the queue, in order to calculate the total number of rabbits next month -Arr.push (Arr.reduce (sum, value) ={ +                     returnSum +value; -}, 0)); +  A                //The rabbit has been calculated for the current month, so we no longer need to know the logarithm of the rabbit last month, so we kicked it out of the queue. at Arr.shift (); -             } -  -           //The Fibonacci Array holds the logarithm of the rabbit every month, as long as it returns to len-1 months. -             returnFibonacci[len-1]; -}

Then we can use another method to achieve, by the variable loop assignment value to calculate, this is relatively simple.

function fibonacci_2 (len) {            = 1,//last month                 = 1,//One months                 null,  //Current Month                 = [1, 1];                                  for (var i = 0; i< len-2; i++) {                    = a +b;                    Arr.push (c);                     = B;                      = c;                }             return arr[len-1];      }

There is also a more common recursive approach:

Const FIRBONACCI =(function () {//Create an array, save the rabbits for the first two monthsConst ARR = [0, 1]; return functionFN (n) {if(N < 0)return ;
If it is the first two months, return the corresponding item of the array directlyif(N < 2)returnArr[n]; if(Arr[n])returnArr[n];
Recursive execution FunctionsreturnFN (n-1) + fn (n-2); } }());

There are many ways to achieve the Fibonacci sequence, using data structure to solve the problem I think it is very interesting thing, the above way of implementation does not consider the complexity of time, space complexity of what, the elder brother write code in a word: roll up sleeves, keyboard, a meal CTRL + C and CTRL + V, is dry!

JavaScript implements different positions of Fibonacci sequences

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.