> -Original Message-
> From: r-help-boun...@r-project.org
> [mailto:r-help-boun...@r-project.org] On Behalf Of Nordlund,
> Dan (DSHS/RDA)
> Sent: Thursday, April 21, 2011 9:19 AM
> To: r-help@r-project.org
> Subject: Re: [R] Fibonacci
>
> > -Original
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of David Winsemius
> Sent: Thursday, April 21, 2011 8:44 AM
> To: Michael Dewey
> Cc: r-help@r-project.org
> Subject: Re: [R] Fibonacci
>
>
> On Apr
On Apr 21, 2011, at 9:04 AM, Michael Dewey wrote:
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
> fi
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)}
>
Thank-you all!!
Very helpful.
-- Forwarded message --
From: Bart Joosen
Date: 20 April 2011 15:46
Subject: Re: [R] Fibonacci
To: r-help@r-project.org
Another solution:
while (Fibonacci[1] < 500) Fibonacci <- c(sum(Fibonacci[c(1,2)]),
Fibonacci)
While this adds the sum
Another solution:
while (Fibonacci[1] < 500) Fibonacci <- c(sum(Fibonacci[c(1,2)]),
Fibonacci)
While this adds the sum before the existing values, the length or tail
function or avoided, but even with reordering, its faster
(Fibonacci[length(Fibonacci):1])
Best regards
Bart
--
View this mess
r-help@r-project.org
Subject: [R] Fibonacci
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)))
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(
The "easy" solution to compute the Fibonacci numbers is
fibo <- function(n,a=1,b=1){
if (n == 1) return(a)
if (n == 2) return(b)
return(fibo(n-1,b,a+b))
}
It avoids double recursion.
It is, however, not as resource efficient as a loop since R does not do
tail recursion elimination.
On Ap
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 m
10 matches
Mail list logo