1 / 5

CSci 160 Lecture 18

CSci 160 Lecture 18. Martin van Bommel. Fibonacci Sequence. Fibonacci investigated how fast rabbits could breed in ideal circumstances Mate at one month, produce pair of rabbits every month from month two on Link on web

kermit
Download Presentation

CSci 160 Lecture 18

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSci 160Lecture 18 Martin van Bommel

  2. Fibonacci Sequence • Fibonacci investigated how fast rabbits could breed in ideal circumstances • Mate at one month, produce pair of rabbits every month from month two on • Link on web • Fibonacci sequence for number of pairs is 1 1 2 3 5 8 13 21 34 55 ...

  3. Fibonacci Equation • Sequence 1 1 2 3 5 8 ... can be definedFib(1) = 1Fib(2) = 1Fib(n) = Fib(n-1) + Fib(n-2) if n > 2

  4. Recursive Fibonacci long int Fib(int n) { if (n <= 2) return (1); return (Fib(n-1) + Fib(n-2)); } • fibr.c • Number of recursive calls to Fib = Fib(n+1)

  5. Non-recursive Fibonacci long int Fib(int n) { int i, n1 = 1, n2 = 1, f = 1; for (i = 3 ; i <= n; i++) { f = n1 + n2; n1 = n2; n2 = f; } return f; }

More Related