title: Fibonacci n-step number sequences with R
body: <p><a href="http://mathworld.wolfram.com/Fibonaccin-StepNumber.html" rel="nofollow">Fibonacci n-step number sequences</a> is a generalization of Fibonacci numbers where you add more than two of the preceding numbers together. I try to write a function in R that does that.</p>
  
  <p>First the classical Fibonacci numbers:</p>
  
  <pre><code>fib &lt;- function(n){
    fib &lt;- vector()
    fib[1] &lt;- 1
    fib[2] &lt;- 1
    for(i in 3:n){
      fib[i] &lt;- fib[i-1] + fib[i-2]
    }
    return(fib)
  }
  
  fib(7)
  ## [1]  1  1  2  3  5  8 13
  </code></pre>
  
  <p>Next the so called Tribonacci numbers:</p>
  
  <pre><code>fib3 &lt;- function(n){
    fib &lt;- vector()
    fib[1] &lt;- 1
    fib[2] &lt;- 1
    fib[3] &lt;- 2
    for(i in 4:n){
      fib[i] &lt;- fib[i-1] + fib[i-2] + fib[i-3]
    }
    return(fib)
  }
  
  fib3(7)
  ## [1]  1  1  2  4  7 13 24
  </code></pre>
  
  <p>and so on...</p>
  
  <p>The initial values are powers of 2, so for n=3 it is 1,1,2, for n=4 it is 1,1,2,4, for n=5 it is 1,1,2,4,8 and so on.</p>
  
  <p>So my idea is to modify the above function on the fly, especially the number of arguments in the for-loop, yet I don't know enough of R to achieve that efficiently.</p>
  
  <p><strong>My question</strong><br>
  How can I modify the above code to create any Fibonacci n-step number sequence?</p>
  
  <p><strong>Additional info</strong><br>
  Many more details and different implementations can be found here... unfortunately not in R: <a href="http://rosettacode.org/wiki/Fibonacci_n-step_number_sequences#Julia" rel="nofollow">Rosetta code Fibonacci n-step number sequences</a></p>