JavaScript fun: Tribonacci Series

Source: Internet
Author: User
Next, I would like to introduce the Tribonacci series, the big brother of the Fibonacci series. Next, I would like to introduce the Tribonacci series, the big brother of the Fibonacci series.
As its name implies, it is similar to the Fibonacci series, but there are differences.
If we start this series with [, 1], we will get the following results:
[,...]
As you can see, item 4 3 is the sum of Items 1, 2, and 3, item 5 is the sum of items 2, 3, and 4, and so on ......
What will happen if we start this series with [, 1?
[, 24,...]
After reading this example, you probably have guessed the rule: Except for the first three digits used as the initial sequence, each digit in the future is equal to the sum of the three digits closest to it.
Then compare the Fibonacci series. The rule is: Except for the first two digits used as the initial sequence, each digit in the future is equal to the sum of the two digits closest to it.
Now, do you understand why I say they are brothers?
Even according to this rule, it can be extended to the Xbonacci series, but I will only talk about the Tribonacci series.
Okay. Think about how to construct a Tribonacci series!
It receives two parameters. The first parameter is an array as the initialization sequence. The second parameter is a number, indicating the number of columns to be generated.
The returned value is an array of Columns after a new item is generated.
There are two different situations for this problem:
First, what if the size of the initialization sequence is 3 but I only need to generate two items? The answer is simple. Just extract the first two items from the initialization array and you will be OK.

In the second case, it is very easy to generate items other than the initialization sequence. Just like generating a Fibonacci series, a cycle and new items are composed of the last three items, and so on.

Function tribonacci (signature, n) {var startPoint = 0; if (n <signature. length) {return signature. slice (0, n);} for (var I = startPoint + signature. length; I
 
  

The above is the content of the Tribonacci series. For more information, see the PHP Chinese website (www.php1.cn )!

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.