Fibonacci n-step number sequences

From Rosetta Code
Revision as of 06:06, 25 May 2012 by rosettacode>Paddy3118 (→‎{{header|Python}}: Catch just IndexError)
Fibonacci n-step number sequences is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 n-step sequence - ; with initial values of the first n values of the (n-1)'th Fibonacci n-step sequence ; and k'th value of this n'th sequence being

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

Fibonacci n-step sequences
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 octanacci 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 preceeding values like the fibonacci series for but has the values as its initial values.
The task is to
  1. Write a function to generate Fibonacci 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.
C.f

Python

<lang python>>>> def fiblike(start): addnum = len(start) def fibber(n): try: return fibber.memo[n] except IndexError: ans = sum(fibber(i) for i in range(n-addnum, n)) fibber.memo.append(ans) return ans fibber.memo = start[:] return fibber

>>> fibo = fiblike([1,1]) >>> [fibo(i) for i in range(10)] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>> lucas = fiblike([2,1]) >>> [lucas(i) for i in range(10)] [2, 1, 3, 4, 7, 11, 18, 29, 47, 76] >>> for n, name in zip(range(2,11), 'fibo tribo tetra penta hexa hepta octa nona deca'.split()) : fibber = fiblike([1] + [2**i for i in range(n-1)]) print('n=%2i, %5snacci -> %s ...' % (n, name, ' '.join(str(fibber(i)) for i in range(15))))


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

Tcl

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6

proc fibber {args} {

   coroutine fib[incr ::fibs]=[join $args ","] apply {fn {

set n [info coroutine] foreach f $fn { if {![yield $n]} return set n $f } while {[yield $n]} { set fn [linsert [lreplace $fn 0 0] end [set n [+ {*}$fn]]] }

   } ::tcl::mathop} $args

}

proc print10 cr {

   for {set i 1} {$i <= 10} {incr i} {

lappend out [$cr true]

   }
   puts \[[join [lappend out ...] ", "]\]
   $cr false

} puts "FIBONACCI" print10 [fibber 1 1] puts "TRIBONACCI" print10 [fibber 1 1 2] puts "TETRANACCI" print10 [fibber 1 1 2 4] puts "LUCAS" print10 [fibber 2 1]</lang>

Output:
FIBONACCI
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...]
TRIBONACCI
[1, 1, 2, 4, 7, 13, 24, 44, 81, 149, ...]
TETRANACCI
[1, 1, 2, 4, 8, 15, 29, 56, 108, 208, ...]
LUCAS
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76, ...]