Docsity

Recursive Definitions of Functions, Summaries of Calculus

Toggle sidebar
bg1
Recursive Definitions of Functions
Recursive Integer Functions
Intuitively, a recursive function fis one whose output can be defined for a given input by equating
its associated output to an expression that includes the output values of ffor inputs of smaller size.
For example, if ftakes as input a nonnegative integer n≥0, then recursive f(n) can be defined by
equating it to an expression that includes values f(m1), . . . , f(ma), where a≥1 and m1, . . . , ma< n.
When this equation is required for computing f(n) for some n, then nis called a recursive case.
However, what if nis already “small”? Say n= 0. Then since there are no smaller numbers than n,
nis called a base case, and f(n) must be defined by equating it to a constant, such as f(0) = 5, or
f(1) = 2, etc..
In what follows we assume that the domain of fis the set of natural numbers N={0,1,2, . . .}. Note
that functions of this kind are often referred to as sequences, since they may also be expressed as
f0, f1, f2, . . ., where fiis shorthand for f(i), i= 0,1,2, . . ..
To summarize, when defining a recursive function, there are two cases to consider:
Base Case when the input nhas a size that is one of the smallest possible. In this case f(n) is
equated to a constant.
Recursive Case f(n) can be defined by equating it to an expression that uses one or more f-values
on inputs that are smaller than n.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Recursive Definitions of Functions and more Summaries Calculus in PDF only on Docsity!

Recursive Definitions of Functions

Recursive Integer Functions

Intuitively, a recursive function f is one whose output can be defined for a given input by equating its associated output to an expression that includes the output values of f for inputs of smaller size. For example, if f takes as input a nonnegative integer n ≥ 0, then recursive f (n) can be defined by equating it to an expression that includes values f (m 1 ),... , f (ma), where a ≥ 1 and m 1 ,... , ma < n. When this equation is required for computing f (n) for some n, then n is called a recursive case. However, what if n is already “small”? Say n = 0. Then since there are no smaller numbers than n, n is called a base case, and f (n) must be defined by equating it to a constant, such as f (0) = 5, or f (1) = 2, etc..

In what follows we assume that the domain of f is the set of natural numbers N = { 0 , 1 , 2 ,.. .}. Note that functions of this kind are often referred to as sequences, since they may also be expressed as f 0 , f 1 , f 2 ,.. ., where fi is shorthand for f (i), i = 0, 1 , 2 ,.. ..

To summarize, when defining a recursive function, there are two cases to consider:

Base Case when the input n has a size that is one of the smallest possible. In this case f (n) is equated to a constant.

Recursive Case f (n) can be defined by equating it to an expression that uses one or more f -values on inputs that are smaller than n.

Example 1. Consider the following recursive definition. Base Case: f (0) = 1. Recursive Case: for n ≥ 1, f (n) = 3f (n − 1) + 2. Use this definition to compute f (1), f (2), and f (3).

Example 3. Provide a recursive definition for f (n) = n!.

Example 4. Provide a recursive definition for the function f (n) = an, where a > 0 is a real constant.

  • Example 6. Draw the recursion tree for Fibonacci number f

One application of a recursion tree for a recursively defined function f for input n is that the number of tree nodes equals the number of calls that need to be made to function f in order to compute f (n), assuming that f (n) is recursively implemented. For example, below a recursive implementation of the Fibonacci sequence fn.

int fib(int n) { if(n <= 1) return n;

return fib(n-1) + fib(n-2); }

Example 7. Let Nn equal the number of nodes in the recursion tree for Fibonacci sequence fn. Show that Nn > fn for all n ≥ 0. Conclude that a recursive implementation of fn, such as fib, will require a number of function calls that exceeds fn.

Exercises

  1. Determine f (1), f (2), f (3), and f (4) if f (n) is recursively defined by f (0) = 1 and, for n = 1 , 2 ,...

a. f (n) = f (n − 1) + 3 b. f (n) = 2f (n − 1) c. f (n) = 2f^ (n−1) d. f (n) = f (n − 1)^2 + 2f (n − 1) + 1

  1. Determine T (2), T (3), T (4), and T (5), if T (n) is recursively defined by T (0) = 2, T (1) = 2, and T (n) = T (n − 1) + 3T (n − 2) + 4.
  2. Draw the recursion tree for T (4), where T is the function from the previous exercise. How many nodes does it have?
  3. Provide a recursive definition of the function f (n) = 2n.
  4. Draw the recursion tree that would form when computing f (5) using your recursive definition from the previous exercise. Label each node with the input being computed at that level of recursion, and next to each node write the output that will be returned for that input.
  5. Provide a recursive definition of the function f (n) = 2n + 1.
  6. Provide a recursive definition of the function f (n) = n^2.
  7. Let fm(n) = mn, where m ≥ 0 is some integer constant. Provide a recursive defintion for fm.
  8. Provide a recursive definition of the function f (n) = n^3.
  9. Let Nn be the sequence defined in Example 7. Use mathematical induction to prove that Nn > fn for all n ≥ 2.
  10. Define f (n) recursively as f (0) = 0, f (1) = 1, and,for n ≥ 2, f (n) = −f (n − 2). Compute f (2),... , f (8). Use mathematical induction to prove that f (n) = sin(πn 2 ), for all n ≥ 0.
  11. Let T (n) be the function defined in Exercise 2. Use mathematical induction to prove that T (n) always ends with the digit 2.

Exercise Solutions

  1. We have

a. f (1) = 4, f (2) = 7, f (3) = 10, and f (4) = 13. b. f (1) = 2, f (2) = 4, f (3) = 8, and f (4) = 16. c. f (1) = 2, f (2) = 4, f (3) = 16, and f (4) = 2^16 = 65536. d. f (1) = 4, f (2) = 25, f (3) = 676, and f (4) = 458329.

  1. We have T (2) = 2 + 3(2) + 4 = 12, T (3) = 12 + 3(2) + 4 = 22, T (4) = 22 + 3(12) + 4 = 62, T (5) = 62 + 3(22) + 4 = 132.
  2. The T (4) recursion tree has 1 node at the root level (level 0), 2 nodes at level 1, 4 nodes at level 2, and 2 nodes at level 3, for a total of 9 nodes. where T is the function from the previous exercise. How many nodes does it have?
  3. Base Case. f (0) = 0. Recursive Case. f (n) = f (n − 1) + 2.
  4. Each node is labeled as n/f (n), where n is the function input at that level of recursion, and f (n) is the returned value at that level.
  1. Base case: f (0) = 1. Recursive Case: f (n) = f (n − 1) + 2.
  2. Base case: f (0) = 0. Recursive Case: since

n^2 = ((n − 1) + 1)^2 = (n − 1)^2 + 2(n − 1) + 1 = (n − 1)^2 + 2n − 1 ,

we have f (n) = f (n − 1) + 2n − 1.

  1. Base Case. fm(0) = 0. Recursive Case. fm(n) = fm(n − 1) + m.