Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

If we begin with a single pair of rabbits, how many pairs will we have ... Whoops, don't want to use int division though. Design: Just the ratio: while count =n ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 20
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • Case StudyFibonacci Numbers

2
Fibonacci Numbers
  • Leonardo Fibonacci
  • Italian mathematician
  • 1202 presented problem about population of
    rabbits (oversimplified)
  • Each pair of rabbits produces new pair each month
  • Rabbits become fertile 1 month after birth
  • Rabbits never die
  • If we begin with a single pair of rabbits, how
    many pairs will we have after n months for
    specified value of n?

3
Some comments
  • It is often important to oversimplify a problem
    in the beginning in order to get a good start on
    the problem.
  • Modeling real world problems is one of the most
    important uses of computers
  • Faster than real experiments
  • Less expensive
  • Modeling usually requires many iterations in
    order to reach the appropriate level of
    complexity.

4
More comments
  • It turns out that the study of the number of
    pairs of rabbits over successive months leads to
    a series of numbers that occur often in nature.
  • There has been a large amount of research done on
    the numbers and their importance in nature.

5
Rabbit family tree
  • Note Each month all pairs from previous month
    survive.
  • The number of new pairs born is same as number of
    pairs two months back
  • 1,1,2,3,5,8,13,

6
Fibonacci Numbers
  • The Fibonacci Numbers are defined by
  • The first two numbers are 1 and 1.
  • Each subsequent number is the sum of the
    preceding 2 numbers.
  • 1,1,2,3,5,8,13,21,34,55,etc.

7
Request and Analysis
  • Request Write program that gives the nth
    Fibonacci number where n is a positive number.
  • Analysis The user will input the positive number
    n and the program will output the nth Fibonacci
    number.
  • Fibonacci number 10 is 55
  • Enter a positive number indicating which
    Fibonacci number you wish to see 10

8
Design
  • Design get input n fib 1 (for
    first 2 numbers) twoBack 1 oneBack
    1 count 3 while count lt n fib
    oneBack twoBack twoBack oneBack oneBack
    fib count count1 output Fib

9
Implementation - Variables
  • KeyboardReader reader new KeyboardReader()Scre
    enWriter writer new ScreenWriter()
  • int n // which Fibonacci
    numberint fib // the current Fib
    numberint oneBack // the previous Fib
    numberint twoBack // the Fib number two
    previousint count // where are we in
    sequence

10
Implementation run - input
  • n reader.readInt(Enter a positive number
    indicating which Fibonacci number you wish to
    see )while (nlt0) n reader.readInt(Positi
    ve, please )

11
Implementation run initialization and loop
  • fib 1twoBack 1oneBack 1count 3
  • while (count lt n) fib oneBack
    twoBack twoBack oneBack oneBack
    fib count count 1

12
Implementation run - output
  • writer.print(Fibonacci number )writer.print(n)
    writer.print( is )writer.println(fib)

13
Testing
14
Maintenance
  • Request Have the program print out each
    Fibonacci number as it goes forgot to tell you
    that.
  • Analysis Easy enough to just put the output
    inside the loop. But be careful not to print
    anyone twice. And dont skip any.
  • Design Just the output output 1
    if ngt1 print another 1 while
    countltn output fib

15
Maintenance
  • Request Oh, Im getting into this stuff now. Id
    like to see the ratios of consecutive Fib
    numbers. Only care about n 3 or more.
  • Analysis Easy enough to just print the ratios
    inside the loop. Whoops, dont want to use int
    division though.
  • Design Just the ratio while
    countltn output fib/oneBack
  • Implementation writer.print(fib1.0/oneBack)

16
Testing
17
Testing
  • Well have to use doubles to go further

18
A Great Web Site on Fib numbers
  • http//www.ee.surrey.ac.uk/Personal/R.Knott/Fibona
    cci/fib.html

19
We're not talking U.S. inspected, we're talking
prime here, boys!
Write a Comment
User Comments (0)
About PowerShow.com