Example

# Example - Fibonacci numbers Fibonacci numbers 0 1 1 2 3 5 8...

This preview shows pages 1–6. Sign up to view the full content.

Fibonacci numbers Fibonacci numbers : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . .. where each number is the sum of the preceding two. Recursive definition: F(0) = 0; F(1) = 1; F(number) = F(number-1)+ F(number- 2);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Redundant Calculations I To compute fib(n), we recursively compute fib(n-1). When the recursive call return, we compute fib(n-2) using another recursive call We have already computed fib(n-2) in the process of computing fib(n-1) We make two calls to fib(n-2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Redundant Calculations II Making two method calls would double the running time Compounding effect: each recursive call does more and more redundant work Each call to fib(n-1) and each call to fib(n-2) makes a call to fib(n-3); there are 3 calls to fib(n-3) Each call to fib(n-2) or fib(n-3) results in a call to fib(n-4), so 5 calls to fib(n-4)
C(n): number of calls to fib method C(0)=C(1)=1; For n>=2, we call fib(n) and plus all the calls needed to evaluate fib(n-1) and fib(n-

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 10

Example - Fibonacci numbers Fibonacci numbers 0 1 1 2 3 5 8...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online