Fibonacci n-step number sequences

From Fōrmulæ wiki
Revision as of 14:57, 23 March 2019 by Admin (Talk | contribs) (Description (from Rosetta Code))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page is the answer to the task Fibonacci n-step number sequences in the Rosetta Code.

Description (from Rosetta Code)

These number series are an expansion of the ordinary Fibonacci sequence where:
  1. For we have the Fibonacci sequence; with initial values and
  2. For we have the tribonacci sequence; with initial values and
  3. For we have the tetranacci sequence; with initial values and
    ...
  4. For general we have the Fibonacci -step sequence - ; with initial values of the first values of the 'th Fibonacci -step sequence ; and 'th value of this 'th sequence being

For small values of , Greek numeric prefixes are sometimes used to individually name each series.

Fibonacci -step sequences
Series name Values
2 fibonacci 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...
3 tribonacci 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ...
4 tetranacci 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ...
5 pentanacci 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ...
6 hexanacci 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ...
7 heptanacci 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ...
8 octonacci 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ...
9 nonanacci 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ...
10 decanacci 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ...

Allied sequences can be generated where the initial values are changed: The Lucas series sums the two preceding values like the fibonacci series for but uses as its initial values.

Task

  1. Write a function to generate Fibonacci -step number sequences given its initial values and assuming the number of initial values determines how many previous values are summed to make the next number of the series.
  2. Use this to print and show here at least the first ten members of the Fibo/tribo/tetra-nacci and Lucas sequences.

Solution

Main function

According to the requirements, the program must generate a series, and the order (Fibonacci, Tribonacci, etc) should be determined according with the initial values.

In this case, the number n indicates how many terms of the series will be generated.

FibonacciN1.png

The following generates a Fibonacci series of 20 terms:

FibonacciN1Output1.png

The following generates a Lucas series of 20 terms:

FibonacciN1Output2.png

The following generates a Tribonacci series of 20 terms:

FibonacciN1Output3.png

Generating initial values

The initial values can be generated by the following function.

FibonacciN2.png

Note that it is a recursive function, and it calls the previously defined function. It requires the initial values as a seed, for Fibonacci style (Fibonacci, Tribonacci, etc), and for Lucas style.

The following generates the initial values for Fibonacci.

FibonacciN2Output1.png

The following generates the initial values for Lucas.

FibonacciN2Output2.png

Generating tables of series for Fibonacci and Lucas

This generates a tables of series for Fibonacci (25 terms), for orders 2 to 20 (Fibonacci, Tribonacci, etc.)

FibonacciN3Output1.png

This generates a tables of series for Lucas (25 terms), for orders 2 to 20

FibonacciN3Output2.png