On Wed, Apr 20, 2011 at 11:42:38AM +0200, 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.
> 
> 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)

Is it allowed to use length() function? If so, then try
the following

  Fibonacci<-c(1,1)
  while (max(Fibonacci)<500){
      Fibonacci<-c(Fibonacci, Fibonacci[length(Fibonacci) - 1] + 
Fibonacci[length(Fibonacci)])
  }

Petr Savicky.

______________________________________________
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