Problem Set 5: Problem 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Problem Set 5: Problem 2

Description:

A naive Fibonacci number generator. Implement an optimized Fibonacci number generator ... 1000th Number: 5.172 ms. The Naive version takes. So long that I did ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 13
Provided by: csUi
Category:
Tags: naive | problem | set

less

Transcript and Presenter's Notes

Title: Problem Set 5: Problem 2


1
Problem Set 5 Problem 2
  • 22C021 Computer Science
  • Data Structures

2
What needs to be done?
  • Implement a Fibonacci Number Generator
  • A naive Fibonacci number generator
  • Implement an optimized Fibonacci number generator
  • That remembers results of sub-problems already
    solved
  • Evaluate Performance

3
Fibonacci Number Generator Naive
  • public static int fibonacci(int n)
  • if((n 1) (n 2))
  • return 1
  • else
  • return fibonacci (n-1) fibonacci (n-2)

4
Fibonacci Number Generator Optimized
  • We need
  • An array answers that stores results of solved
    sub-problems
  • A type that can handle large numbers
  • Fibonacci numbers grow fast, integers and longs
    run out of range
  • A way to check if a sub-problem has already been
    solved
  • Only need to recurse when necessary

5
The BigInteger Data Type
  • Available under java.util namespace
  • Can store really large values
  • Check Java Documentation for more Details about
    this type
  • http//java.sun.com/j2se/1.4.2/docs/api/java/math/
    BigInteger.html

6
The answers array
  • What should be the size of the answers array
  • The size n of this array should be equal to the
    Fibonacci number we've been asked to generate
  • This array stores the nth Fibonacci number at
    n-1th Location
  • private static BigInteger answers

7
Initialize the answers array
  • // Initializing answers
  • int number ltfibonacci number to generategt
  • answers new BigIntegernumber
  • // The first two numbers in series are 1
  • answers0 new BigInteger("1")
  • answers1 new BigInteger("1")
  • // Set all others to zeros to mark them as
  • // not-computed
  • for(int i 2 i lt number i)
  • answersi new BigInteger("0")

8
Optimized Fibonacci Number Generator
  • public static BigInteger fastFibonacci(int n)
  • if((n 1) (n 2))
  • return answers0
  • if(answersn-1.compareTo(zero) ! 0)
  • return answersn-1
  • if(answersn-2.compareTo(zero) 0)
  • answersn-2 fastFibonacci(n-1)
  • if(answersn-3.compareTo(zero) 0)
  • answersn-3 fastFibonacci(n-2)
  • return answersn-2.add(answersn-3)

9
Optimized Fibonacci Number Generator
  • When asked to generate a number
  • We check if the number requested has already been
    computed and return it if it has been.
  • Then, we check if the required numbers have
    already been computed (n 1 and n - 2)
  • If they have, we straightaway use their values
  • If they haven't, we call the generator number on
    each of the missing required numbers
  • Once we have both the value, we add them and
    return the sum

10
Performance Comparisons
  • How fast is the optimized version?
  • To generate the 46th Fibonacci Number
  • The unoptimized version takes 885490.7 ms
    Approx 15 minutes
  • The optimized version takes 0.145 ms

11
Performance Comparison
12
Other Performance Stats
  • The Optimized version takes
  • For 100th Number 0.329 ms
  • For 1000th Number 5.172 ms
  • The Naive version takes
  • So long that I did not evaluate ...
Write a Comment
User Comments (0)
About PowerShow.com