HO32 - Maggie Johnson CS103A Handout #32 Recursion Key...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Maggie Johnson Handout #32 CS103A Recursion Key topics: * Recursive Definitions * Recursive Subprograms * Proving Properties of Recursive Programs Recursive Definitions One of the most important tasks in computer science is to discover and characterize regular patterns, such as those associated with processes that are repeated. In math, as we have seen, such patterns are called sequences . There are three ways to define a sequence: 1) enumerate the items in the sequence and hope the pattern is obvious; 2) give a formula for the elements in the sequence, for example, the sequence of powers of 2 is given by a(n) = 2 n ; 3) give a recursive or inductive definition with the following parts: a) the initial condition or basis which defines the first (or first few) elements of the sequence; b) an inductive step in which later terms in the sequence are defined in terms of earlier terms. This is called a recurrence relation . Here are some examples of these types of definitions (mostly recurrence relations): 1) We’ll start with the obvious: The set of even numbers can be defined as: 1) 2, 4, 6, 8, 10. ... 2) 2k for some integer k 3) basis: 2 is in EVEN induction: if x is in EVEN then so is x + 2 2) Compound interest can be defined recursively as: basis: A(0) = initial amount induction: A(k) = interest rate * A(k-1) For example, if the initial amount is $1000 and the interest rate is 1.055, after 21 years we get: A(0) = 1000 A(1) = 1.055 * A(0) = 1055.00
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
... A(20) = 1.055 * A(19) = 2917.76 A(21) = 1.055 * A(20) = 3078.23 3) The product of the positive integers from 1 to n, inclusive, called "n factorial" is denoted by n!: multiply 1 * 2 * . .. * n = n! (this is an iterative definition). To devise a recursive definition, notice: 1! = 1 2! = 1 * 2 = 2 3! = 1 * 2 * 3 = 6 4! = 1 * 2 * 3 * 4 = 24 Observe that 4! = 4 * 3! or in general: n! = n * (n - 1)!. Factorial is defined recursively as follows: basis: 1! = 1 induction: n! = n * (n - 1)! For example: 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 2! = 2 * 1 3! = 3 * 2 4! = 4 * 6 By the way, inductive proofs are very useful when we are working with recursive definitions. For example, we can prove using induction that the recursive definition of factorial is equivalent to the iterative one: Prove P(n): n! as defined recursively equals 1 * 2 * . .. * n i) base case: prove that P(1) is true: 1!=1 and 1 * 1 = 1. ii) induction hypothesis is to assume P(k): k! as defined recursively equals 1 * 2 * . .. * k and show P(k+1): (k + 1)! as defined recursively equals 1 * 2 * . .. * k * (k + 1). PROOF : (k + 1)! = (k + 1) * ((k + 1) - 1)! substitution of (n+1) in recursive definition (k + 1) * k! subtraction k! * (k + 1) commutative law for * (1 * 2 * . .. * k) * (k + 1) inductive hypothesis P(k+1) is true when P(k) is true, and therefore the recursive and iterative definitions are equivalent. 4)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

HO32 - Maggie Johnson CS103A Handout #32 Recursion Key...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online