# Re: [R] Fibonacci

From: Nordlund, Dan (DSHS/RDA) <NordlDJ_at_dshs.wa.gov>
Date: Thu, 21 Apr 2011 09:18:56 -0700

> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-
> project.org] On Behalf Of David Winsemius
> Sent: Thursday, April 21, 2011 8:44 AM
> To: Michael Dewey
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Fibonacci
>
>
> 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
> > > 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
>
> I like it!
>
> > test <-c( 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
> > test == fib(1:10)
> [1] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> To avoid such FAQ-tual puzzlement, it might be better to round to zero
> digits:
>
> fib2 <- function(n) {round( (phi ^ n - (1 - phi) ^ n) / sqrt(5) ) }
>
> > test == fib2(1:10)
> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>
>
> There are several packages that offer fib() functions of one name or
> another, including the gmp package which always seems to "think
> bigger" than I do. I cannot (trivially) see how that author does it
> with fibnum(), because it is in a .Call()
>
> --
> David
> >
> > 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
> > info_at_aghmed.fsnet.co.uk
> >
http://www.aghmed.fsnet.co.uk/home.html
> >

I, too, like the Michael/David/Wikipedia solution. But if this is homework, I would recommend using length() instead of max() for getting the last two items of the vector.

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division
Olympia, WA 98504-5204

R-help_at_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. Received on Thu 21 Apr 2011 - 16:24:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Apr 2011 - 17:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.