Skip to content
Back to formatted view

Raw Message

Message-ID: <20110420105257.GA10788@praha1.ff.cuni.cz>
Date: 2011-04-20T10:52:57Z
From: Petr Savicky
Subject: Fibonacci
In-Reply-To: <BANLkTim7z2LwyGZRkPYqReT04K1asO615w@mail.gmail.com>

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.