At 10:42 20/04/2011, Georgina Imberger wrote:
Hi!

I am trying to work out the code to get a Fibonacci sequence, using the
while() loop and only one variable. And I can't figure it out.


> phi <- 0.5 * (1 + sqrt(5))
> phi
[1] 1.618034
> fib <- function(n) {(phi ^ n - (1 - phi) ^ n) / sqrt(5)}
> fib(1:10)
 [1]  1  1  2  3  5  8 13 21 34 55
>

Admittedly this does not use a while loop as you requested

Courtesy of Wikipedia


Fibonacci<-c(1,1)
while (max(Fibonacci)<500){
Fibonacci<-c(Fibonacci, (max(Fibonacci) + ?(Fibanacci)))
}


How can I tell R to take the value one before the max value? (Without
defining another variable)

(Probably super easy... I am a beginner...)

Thanks,
Georgie

        [[alternative HTML version deleted]]

Michael Dewey
i...@aghmed.fsnet.co.uk
http://www.aghmed.fsnet.co.uk/home.html

______________________________________________
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.

Reply via email to