Lesson 21: Recursion vs' Iteration - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Lesson 21: Recursion vs' Iteration

Description:

License Plates. Assume each state follows this format for their license plates: AIB 346. how many license plates could be generated throughout the US? Sum Rule ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 14
Provided by: jwi6
Category:

less

Transcript and Presenter's Notes

Title: Lesson 21: Recursion vs' Iteration


1
Lesson 21 Recursion vs. Iteration
  • Objectives
  • Distinguish between an iterative algorithm and a
    recursive algorithm
  • Write recursive algorithms for performing
    mathematical tasks
  • Outline
  • Recursive algorithms
  • Iterative algorithms

2
Recursive Functions
  • An algorithm is called recursive if it solves a
    problem by reducing it to an instance of the same
    problem with a lower input size.
  • Such an algorithm must include a BASE CASE
  • Such an algorithm must include a rule for taking
    a standard (non-base case) input and breaking it
    down into a smaller problem.

3
Recursive Functions
  • //computes an recursively
  • int pow (int a, int n)

4
Greatest Common Divisor
  • GCD(a,b) a lt b
  • Standard List all of the factors of each a, b.
  • Recursive algorithm

5
Recursive calculation of factorial
  • factorial n! n(n-1)(n-2)...1
  • int rfact (int x)

6
Iteration
  • Iteration involves sequentially calculating from
    the ground up and usually involves for loops.
  • int ifact (int x)

7
Counting (chapter 4)
  • Importance of counting
  • Allocation of Resources
  • Probability
  • Security
  • Collisions

8
Counting
  • Product Rule
  • If there are n ways to make choice 1 and m ways
    to make choice 2, then the total number of
    choices is
  • mn

9
Computer passwords
  • If lowercase alphabetic characters are allowed,
    how many 8-character passwords are there?
  • If upper- and lower-case and numeric characters
    are allowed, how many 10-character passwords are
    there?

10
MAC Addresses
  • Ethernet addresses consist of a 48-bit MAC
    address hard-coded into each network card. How
    many possible unique cards can be produced with
    this system?
  • If the first 24 bits uniquely identifies the
    vendor of the card (which it does), how many
    cards can be associated with each vendors ID?
  • How should 3Com respond to this?

11
License Plates
  • Assume each state follows this format for their
    license plates
  • AIB 346
  • how many license plates could be generated
    throughout the US?

12
Sum Rule
  • If a first task can be done n ways and a second
    task m ways, but the tasks cannot be done at the
    same time, there are nm choices.

13
Sum Rule
  • A character used in a certain font can either be
    an upper-case Latin character, lower-case English
    character, upper-case Greek character, or a
    Hiragana character.
  • How many possible characters are there?
Write a Comment
User Comments (0)
About PowerShow.com