user
These number series are an expansion of the ordinary Fibonacci sequence where:

1. For 

   n
   =
   2
   {\displaystyle {\displaystyle n=2}}
    we have the Fibonacci sequence; with initial values 

   [
   1
   ,
   1
   ]
   {\displaystyle {\displaystyle [1,1]}}
    and 

   F

   k

   2
   =

   F

   k
   −
   1

   2
   +

   F

   k
   −
   2

   2
   {\displaystyle {\displaystyle F\_{k}^{2}=F\_{k-1}^{2}+F\_{k-2}^{2}}}
2. For 

   n
   =
   3
   {\displaystyle {\displaystyle n=3}}
    we have the tribonacci sequence; with initial values 

   [
   1
   ,
   1
   ,
   2
   ]
   {\displaystyle {\displaystyle [1,1,2]}}
    and 

   F

   k

   3
   =

   F

   k
   −
   1

   3
   +

   F

   k
   −
   2

   3
   +

   F

   k
   −
   3

   3
   {\displaystyle {\displaystyle F\_{k}^{3}=F\_{k-1}^{3}+F\_{k-2}^{3}+F\_{k-3}^{3}}}
3. For 

   n
   =
   4
   {\displaystyle {\displaystyle n=4}}
    we have the tetranacci sequence; with initial values 

   [
   1
   ,
   1
   ,
   2
   ,
   4
   ]
   {\displaystyle {\displaystyle [1,1,2,4]}}
    and 

   F

   k

   4
   =

   F

   k
   −
   1

   4
   +

   F

   k
   −
   2

   4
   +

   F

   k
   −
   3

   4
   +

   F

   k
   −
   4

   4
   {\displaystyle {\displaystyle F\_{k}^{4}=F\_{k-1}^{4}+F\_{k-2}^{4}+F\_{k-3}^{4}+F\_{k-4}^{4}}}
     
   ...
4. For general 

   n
   >
   2
   {\displaystyle {\displaystyle n>2}}
    we have the Fibonacci 

   n
   {\displaystyle {\displaystyle n}}
   -step sequence - 

   F

   k

   n
   {\displaystyle {\displaystyle F\_{k}^{n}}}
   ; with initial values of the first 

   n
   {\displaystyle {\displaystyle n}}
    values of the 

   (
   n
   −
   1
   )
   {\displaystyle {\displaystyle (n-1)}}
   'th Fibonacci 

   n
   {\displaystyle {\displaystyle n}}
   -step sequence 

   F

   k

   n
   −
   1
   {\displaystyle {\displaystyle F\_{k}^{n-1}}}
   ; and 

   k
   {\displaystyle {\displaystyle k}}
   'th value of this 

   n
   {\displaystyle {\displaystyle n}}
   'th sequence being 

   F

   k

   n
   =

   ∑

   i
   =
   1

   (
   n
   )


   F

   k
   −
   i

   (
   n
   )
   {\displaystyle {\displaystyle F\_{k}^{n}=\sum \_{i=1}^{(n)}{F\_{k-i}^{(n)}}}}

For small values of 

n
{\displaystyle {\displaystyle n}}
, Greek numeric prefixes are sometimes used to individually name each series.

:   :   :   :   Fibonacci 

                n
                {\displaystyle {\displaystyle n}}
                -step sequences

                | n {\displaystyle {\displaystyle n}} | 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 

    n
    =
    2
    {\displaystyle {\displaystyle n=2}}
     but uses 

    [
    2
    ,
    1
    ]
    {\displaystyle {\displaystyle [2,1]}}
     as its initial values.

Task

1. Write a function to generate Fibonacci 

   n
   {\displaystyle {\displaystyle n}}
   -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.

Related tasks

* Fibonacci sequence
* Wolfram Mathworld
* Hofstadter Q sequence‎
* Leonardo numbers

Also see

* Lucas Numbers - Numberphile (Video)
* Tribonacci Numbers (and the Rauzy Fractal) - Numberphile (Video)
* Wikipedia, Lucas number
* MathWorld, Fibonacci Number
* Some identities for r-Fibonacci numbers
* OEIS Fibonacci numbers
* OEIS Lucas numbers
assistant
(formerly Perl 6)

### Lazy List with Closure

```
sub nacci ( $s = 2, :@start = (1,) ) {
    my @seq = |@start, { state $n = +@start; @seq[ ($n - $s .. $n++ - 1).grep: * >= 0 ].sum } … *;
}

put "{.fmt: '%2d'}-nacci: ", nacci($_)[^20] for 2..12 ;

put "Lucas: ", nacci(:start(2,1))[^20];

```

Output:

```
 2-nacci: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
 3-nacci: 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 19513 35890 66012
 4-nacci: 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 10671 20569 39648 76424 147312
 5-nacci: 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 13624 26784 52656 103519 203513
 6-nacci: 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 15109 29970 59448 117920 233904
 7-nacci: 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 15808 31489 62725 124946 248888
 8-nacci: 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 16128 32192 64256 128257 256005
 9-nacci: 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 16272 32512 64960 129792 259328
10-nacci: 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 16336 32656 65280 130496 260864
11-nacci: 1 1 2 4 8 16 32 64 128 256 512 1024 2047 4093 8184 16364 32720 65424 130816 261568
12-nacci: 1 1 2 4 8 16 32 64 128 256 512 1024 2048 4095 8189 16376 32748 65488 130960 261888
Lucas: 2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349
```

### Generative

A slightly more straight forward way of constructing a lazy list.

**Works with**: Rakudo version 2015.12

```
sub fib ($n, @xs is copy = [1]) {
    flat gather {
        take @xs[*];
        loop {
            take my $x = [+] @xs;
            @xs.push: $x;
            @xs.shift if @xs > $n;
        }
    }
}

for 2..10 -> $n {
    say fib($n, [1])[^20];
}
say fib(2, [2,1])[^20];

```