# [R] Return a vector in Fibonacci sequence function

Michael Dewey info at aghmed.fsnet.co.uk
Mon May 13 15:30:36 CEST 2013

```At 20:51 10/05/2013, Tomos_D wrote:
>That's great thanks

There is a closed form expression for the nth Fibonacci number.
See Wikipedia for details.

>--- Original Message ---
>
>From: "arun kirshna [via R]" <ml-node+s789695n4666758h90 at n4.nabble.com>
>Sent: May 10, 2013 3:34 PM
>To: "Tomos_D" <vampire_love at hotmail.co.uk>
>Subject: Re: Return a vector in Fibonacci sequence function
>
>
>
>Hi,
>May be this helps:
>fibv =function(n)
>{
>f1 = f2 = 1
>f3<- c(f1,f2)
>for(i in seq(2, n-1)) {
>if(n == 0 || n == 1) return(n)
>if(n == 2) return(1)
>
>f = f1 + f2
>f2 = f1
>f1 = f
>f3<- c(f3,f)
>}
>f3
>}
>fibv(0)
># 0
>  fibv(1)
># 1
>  fibv(3)
># 1 1 2
>  fibv(10)
># Â  1  1  2  3  5  8 13 21 34 55
>A.K.
>
>
> >I've written the following piece of code, which returns the nth
>Fibonacci number - how do i adapt it to return all the numbers in the
>sequence up to n, >rather than a single value?
> >
> >fibv =
> >function(n)
> >{
> >if(n == 0 || n == 1) return(n)
> >if(n == 2) return(1)
> >f1 = f2 = 1
> >for(i in seq(2, n-1)) {
> >f = f1 + f2
> >f2 = f1
> >f1 = f
> >}
> >f
> >}
> >fibv()
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>_______________________________________________
>http://r.789695.n4.nabble.com/Return-a-vector-in-Fibonacci-sequence-function-tp4666756p4666758.html
>
>To unsubscribe from Return a vector in Fibonacci
>sequence function, visit
>http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4666756&code=dmFtcGlyZV9sb3ZlQGhvdG1haWwuY28udWt8NDY2Njc1Nnw1NzI5NzYzNjM=
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Return-a-vector-in-Fibonacci-sequence-function-tp4666756p4666796.html
>Sent from the R help mailing list archive at Nabble.com.
>         [[alternative HTML version deleted]]

Michael Dewey
info at aghmed.fsnet.co.uk
http://www.aghmed.fsnet.co.uk/home.html

```