sequences, mathematical induction, and recursionartale/dml/lectures/slides3-sequences.pdf5 example 1...
TRANSCRIPT
Alessandro Artale – UniBZ - http://www.inf.unibz.it/∼artale/ - Lect.3
CHAPTER 5
SEQUENCES, MATHEMATICAL INDUCTION, AND
RECURSION
Copyright © Cengage Learning. All rights reserved.
Sequences SECTION 5.1
3
Sequences We typically represent a sequence as a set of elements written in a row. In the sequence denoted each individual element ak is called a term.
4
Sequences The notation: denotes an infinite sequence. An explicit formula (or general formula) for a sequence is a rule that shows how the value ak depends on k. The following example shows that it is possible for two different explicit formulas to obtain sequences with the same terms.
5
Example 1 – Finding Terms of Sequences Given by Explicit Formulas
Define sequences a1, a2, a3,… and b2, b3, b4,… by the following explicit formulas:
Compute the first five terms of both sequences. Solution:
6
Example 1 – Solution As you can see, the first terms of both sequences are ;; in fact, it can be shown that all terms of both sequences are identical.
cont’d
7
Summation Notation
8
Summation Notation In 1772 the French mathematician Joseph Louis Lagrange introduced the capital Greek letter sigma, Σ, to denote the word sum (or summation), and defined the summation notation as follows:
9
Often, the terms of a summation are expressed using an explicit formula. For instance, it is common to see summations such as
Summation Notation
10
Example 6 – Changing from Summation Notation to Expanded Form
Write the following summation in expanded form:
Solution:
11
Summation Notation A more mathematically precise definition of summation, called a recursive definition, is the following: If m is any integer, then
When solving problems, it is often useful to rewrite a summation using the recursive form of the definition.
12
Product Notation
13
Product Notation The notation for the product of a sequence of numbers is analogous to the notation for their sum. The Greek capital letter pi, , denotes a product. For example,
14
Product Notation A recursive definition for the product notation is the following: If m is any integer, then
15
Example 11 – Computing Products Compute the following products:
a. b. Solution:
a. b.
16
Properties of Summations and Products
17
Properties of Summations and Products
The following theorem states general properties of summations and products.
18
Factorial and “n Choose r” Notation
19
Factorial and “n Choose r ” Notation The product of all consecutive integers up to a given integer occurs so often in mathematics that it is given a special notation—factorial notation.
20
Factorial and “n Choose r ” Notation A recursive definition for factorial is the following: Given any nonnegative integer n,
21
An important use for the factorial notation is in calculating values of quantities, called n choose r, that occur in many branches of mathematics and computer science, especially those connected with the study of counting techniques and probability. Observe that the definition implies that will always be an integer because it is a number of subsets.
Factorial and “n Choose r ” Notation
22
The computational formula: The quantities are also called combinations. Sometimes they are referred to as binomial coefficients because of the connection with the binomial theorem.
Factorial and “n Choose r ” Notation
23
Example 17 – Computing by Hand
Use the formula for computing to evaluate the following expressions:
a. b. c. Solution:
a.
24
Example 17 – Solution b.
The fact that 0! = 1 makes this formula computable. It gives the correct value because a set of size 4 has exactly one subset of size 4, namely itself. c.
cont’d
25
Sequences in Computer Programming
26
Sequences in Computer Programming
An important data type in computer programming consists of finite sequences. In computer programming contexts, these are usually referred to as one-dimensional arrays.
For example, consider a program that analyzes the salaries paid to a sample of 50 workers. Such a program might compute the average salary and the difference between each individual salary and the average.
Each salary is stored in a one-dimensional array:
27
Sequences in Computer Programming
The recursive definitions for summation, product, and factorial lead naturally to computational algorithms. For instance, we present two sets of pseudocode to find the sum of a[1], a[2], …, a[n].
28
Sequences in Computer Programming
In both cases the output is The one on the left exactly mimics the recursive definition; the one on the right is instead an iterative algorithm.
Recursive Iterative int funct S(m,n) if m=n then return a[m]
else return S(m,n-1)+a[n]
29
Application: Algorithm to Convert from Base 10 to Base 2 Using
Repeated Division by 2
30
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
A systematic algorithm to convert any non-negative integer to binary notation uses repeated division by 2. Suppose a is a non-negative integer. Divide a by 2 to obtain a quotient q[0] and a remainder r [0]. If the quotient is nonzero, divide by 2 again to obtain a new quotient q[1] and a new remainder r [1]. Continue this process until a quotient of 0 is obtained. At each stage, the remainder must be less than the divisor, which is 2. Thus each remainder is either 0 or 1.
31
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
The process is illustrated below for a = 38. (Read the divisions from the bottom up.)
The results of all these divisions can be written as a sequence of equations:
32
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
By repeated substitution, then,
33
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
Note that each coefficient of a power of 2 on the right-hand side is one of the remainders obtained in the repeated division of 38 by 2.
This is true for the left-most 1 as well, because 1 = 0 � 2 + 1. Thus
34
Example 19 – Converting from Decimal to Binary Notation Using Repeated Division by 2
Use repeated division by 2 to write the number 2910 in binary notation.
Solution: Hence 2910 = (r[4] r[3] r[2] r[1] r[0])2 = 111012.
35
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
Input: n [a nonnegative integer]
Algorithm Body:
q := n, i := 0 [Repeatedly perform the integer division of q by 2 until q becomes 0. Store successive remainders in a one-dimensional array r [0], r [1], r [2], …. , r [k].
Even if the initial value of q equals 0, the loop should execute one time (so that r [0] is computed).
Thus the guard condition for the while loop is i = 0 or q ≠ 0.]
36
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
while (i = 0 or q ≠ 0)
r [ i ] := q mod 2
q := q div 2
[r [ i ] and q can be obtained by calling the division algorithm.]
i := i + 1
end while
37
Application: Algorithm to Convert from Base 10 to Base 2 Using Repeated Division by 2
[After execution of this step, the values of r [0], r [1], …, r [i − 1] are all 0’s and 1’s, and a = (r [i − 1] r [i − 2] … r [2] r [1]r [0])2.]
Output: r [0], r [1], r [2], ..., r [i − 1] [a sequence of integers]