Fibonacci - PowerPoint PPT Presentation

About This Presentation
Title:

Fibonacci

Description:

The Fibonacci Series is of interest and excitement to Mathematicians and Scientists. ... To calculate a Fibonacci Number simply add the two previous numbers together. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 51
Provided by: Jim4220
Category:

less

Transcript and Presenter's Notes

Title: Fibonacci


1
Fibonacci
  • Problem Solving and Thinking in Engineering
    Programming
  • H. James de St. Germain

2
Understand the Problem
  • The Fibonacci Series is of interest and
    excitement to Mathematicians and Scientists.
  • The Series is
  • 0,1,1,2,3,5,8,13,21,34,55,89,
  • To calculate a Fibonacci Number simply add the
    two previous numbers together.
  • We always start with zero and one (0 and 1)

3
What is the Requirements
  • High Level English Description (or Pseudocode
    Version 1)
  • Calculate and Display the first X Fibonacci
    Numbers

4
Really Understand the Problem
  • Start with 0 and 1 (by definition)
  • Start of sequence is 0,1
  • Add these two together 1
  • Expanded sequence is 0,1,1
  • Add last two numbers together
  • 11 2
  • Expanded sequence is 0,1,1,2
  • Add last two numbers together
  • 12 3
  • Expanded sequence is 0,1,1,2,3

5
Do it by Hand!
  • 0 ? add the first number
  • 1 ? to the second number
  • 1 ? to get the next number
  • Now What?

6
Do it by Hand!
  • 0
  • 1 ? now add this number
  • 1 ? to this number
  • 2 ? to get the next number
  • Now What?

7
Do it by Hand!
  • 0
  • 1
  • 1 ? now add this number
  • 2 ? to this number
  • 3 ? to get the next number
  • Now What?

8
Do it by Hand!
  • 0
  • 1
  • 1
  • 2 ? now add this number
  • 3 ? to this number
  • 5 ? to get the next number
  • Now What?

9
Do it by Hand!
  • 0
  • 1
  • 1
  • 2
  • 3 ? now add this number
  • 5 ? to this number
  • 8 ? to get the next number
  • Now What?

10
What does the Programneed to know at Each step?
  • 0
  • 1
  • 1
  • 2
  • 3 ? now add this number
  • 5 ? to this number
  • 8 ? to get the next number
  • the previous number
  • the number before that
  • the current number

11
What happens at each step?
  • Pseudocode Version 2
  • set the first number to 0
  • set the second number to 1
  • Add previous two numbers together to get current
    number
  • repeat step 3 until done
  • Question
  • Are the last two numbers always the same?

12
Transform Repeat to While
  • Add previous two numbers together to get current
    number
  • repeat step 1 until done
  • while not done
  • Add previous two numbers together to get current
    number
  • end

13
What informatino do we need to know or
compute at Each Step?
  • 2nd Previous Number
  • Previous Number
  • Current Number
  • We need VARIABLES to store each of these

14
Create Variables for our Program
  • second_previous 0
  • previous 1
  • current_number ????
  • current_number second_previous previous

15
What happens at each step?
  • Add previous and 2nd previous numbers to get the
    current Fibonacci number
  • Then update our previous variables to contain
    the new previous numbers
  • Question Is the ordering of these two steps
    important?
  • Is the ordering of the two operations in step 2
    important?

16
Which of these produces the correct values in our
variables?
  • Now is it
  • current second_previous previous
  • previous current
  • second_previous previous
  • Or is it
  • current second_previous previous
  • second_previous previous
  • previous current

17
Lets Confirm our Understandingprevious 1,
second_previous1
  • Case 1
  • current second_previous previous
  • current is assigned the value 2
  • previous current
  • previous is assigned the value 2
  • second_previous previous
  • second_previous is assigned the value 2

18
Lets Confirm our Understandingprevious 1,
second_previous1
  • Case 2
  • current second_previous previous
  • current is assigned the value 2
  • second_previous previous
  • 2nd previous is assigned the value 1
  • previous current
  • previuos is assigned the value 2

19
Pseudocode ( 3rd Version)
  1. print 0,1
  2. set the first two values to 0 and 1
  3. While we havent reached our goal
  4. add these values to get the next (or current)
    value
  5. print the current value
  6. update the previous two values

20
Onward to Code
  • fprintf(0, 1, )
  • second_previous 0
  • previous 1
  • current previous second_previous
  • fprintf(d, , current)

21
Sample Code
  • second_previous 0
  • previous 1
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous
  • fprintf(d, , current)

22
Sample Code
  • second_previous 0
  • previous 1
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous
  • fprintf(d, , current)

23
Sample Code
  • second_previous 0
  • previous 1
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous
  • fprintf(d, , current)
  • second_previous previous
  • previous current
  • current previous second_previous

24
Seems like the same old same old, over and over
and over
  • This implies that we want a loop!
  • Remember A Loop lets the computer do things over
    and over again so we dont have to!
  • What loop to use?
  • For loop or While loop?
  • Give a valid reason to use either!

25
While Loop
  • while ( current lt some large number)
  • Use a while loop because we want all Fibonacci
    numbers less than some number

26
FOR loop
  • for ith_fib_number 31000
  • Use a for loop because we want the first 1000
    Fibonacci numbers

27
Pseudocode (4th version)Very Close to Code
  • Set second_previous to 0
  • Set previous to 1
  • Starting with 3, go until X (by ones)
  • Current value is set to second_previous
    previous
  • Print current value
  • Set second_previous to previous
  • Set previous to current

28
Code
  • second_previous 0
  • previous 1
  • fprintf(d d , second_previous, previous)
  • for I 3total_fib_numbers
  • current second_previous previous
  • fprintf(d , current)
  • second_previous previous
  • previous current
  • end the for loop

29
Thoughts
  • Is the variable I used in the loop?
  • Nope! Its just a place holder.
  • for I 3total_fib_numbers
  • current second_previous previous
  • fprintf(d , current)
  • second_previous previous
  • previous current
  • end the for loop

30
Thoughts
  • Are we calculating anything?
  • Sort of, but when the program is over, does the
    computer have anything it can use?
  • Nope
  • How would we write code to save these values?
  • What data type?

31
Saving the values
  • What would we do if we needed to save the values
    instead of simply printing them to the screen?
  • Answer
  • Use an Array
  • Note now the variable I is important

32
New Code with Array
  • Pre-allocate (save buckets for)
  • enough space for all the numbers
  • fib_numbers zeros(1,total_fib_numbers)
  • Set up the first two fib numbers from memory
  • (your memory)
  • fib_numbers(1) 0
  • fib_numbers(2) 1

33
New Code with Array
for i 3total_fib_numbers fib_numbers(i)
fib_numbers(i-1) fib_numbers(i-2) end
for where did the previous and 2nd previous
variables go?
34
What is wrong with this code?
fib_numbers fib_numbers(i-1)
fib_numbers(i-2) Corrected fib_numbers(i)
fib_numbers(i-1) fib_numbers(i-2) Notice
the Update of the Array uses the (i) next to
the array variable
35
Let me Repeat!
  • NEVER use
  • array 5 6
  • ALWAYS use
  • array( position ) 5 6
  • You must always index into an array!

36
Function
  • How would we turn this code into a function?
  • What are the inputs?
  • What are the outputs?

37
Draw a Black Box
  • You have 1 minutes to draw a black box for this
    function

38
Function as Black Box
Function
Input
Output
39
Fibonacci as Black Box
Fibonacci
Fibonacci Numbers
Count
Array of Numbers
Integer
Compute the first count fibonacci numbers
40
Comment Your Function
  • You have 1 minute to write a brief comment that
    would go at the top of your .m file for the
    Fibonacci function

41
Function Comment
  • array_of_fib_numbers compute_fib(
  • how_many)
  • Author H. James de St. Germain
  • Date Fall 2007
  • This function produces an array of the
  • first how many Fibonacci Numbers

42
Function Design Pattern
  • You have one minute to write the function design
    pattern for this function

43
Function Design Pattern
  • function result_array compute_fib( how_many )
  • result_array(1) 0
  • end function

44
Function Code
  • From your memory and your notes write out the
    code for this function.
  • you have 1 minute.
  • Pseudocode
  • set up first two values in array
  • loop updating the current value based on the
    previous two values

45
Function Code
  • function result_array compute_fib( count )
  • result_array(1) 0
  • result_array(2) 1
  • for counter 3 count
  • result_array(counter)
  • result_array(counter-1)
  • result_array(counter-2)
  • end for loop
  • end function

46
How many
  • semicolons (s) in the function? Where?
  • function result_array compute_fib( count )
  • result_array(1) 0
  • result_array(2) 1
  • for counter 3 count
  • result_array(counter)
  • result_array(counter-1)
  • result_array(counter-2)
  • end for loop
  • end function

47
How many
  • fprintfs and input statements?
  • function result_array compute_fib( count )
  • result_array(1) 0
  • result_array(2) 1
  • for counter 3 count
  • result_array(counter)
  • result_array(counter-1)
  • result_array(counter-2)
  • end for loop
  • end function

48
NEVER
  • use fprintf in a function
  • unless told that the function communicates with
    the user of the program
  • use input in a function
  • unless told that the function recieves input
    from the user of the program

49
How many
  • variables? (parameters, return variables, local
    variables)
  • function result_array compute_fib( count )
  • result_array(1) 0
  • result_array(2) 1
  • for counter 3 count
  • result_array(counter)
  • result_array(counter-1)
  • result_array(counter-2)
  • end for loop
  • end function

50
End Fibonacci
  • Questions?
Write a Comment
User Comments (0)
About PowerShow.com