That's great thanks

--- Original Message ---

From: "arun kirshna [via R]" <ml-node+s789695n4666758h90@n4.nabble.com>
Sent: May 10, 2013 3:34 PM
To: "Tomos_D" <vampire_love@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)
#[1] 0
 fibv(1)
#[1] 1
 fibv(3)
#[1] 1 1 2
 fibv(10)
# [1]  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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




_______________________________________________
If you reply to this email, your message will be added to the discussion below:
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]]

