You are on page 1of 35

SEQUENCES AND

SUMMATIONS

1
Sequences
 Sequences represent ordered lists of elements.

Definition: A sequence is a function from a subset of the integers


(usually either the set {0, 1, 2, 3, 4, …..} or {1, 2, 3, 4, ….} ) to a set S.

 The notation an is used to denote the image of the integer n. We can


think of an as the equivalent of f(n) where f is a function from
{0,1,2,…..} to S. We call an a term of the sequence.

Example:
Consider the sequence , where .
Therefore the terms of the sequence are
The Equation Game

What are the equations that describe the following sequences a1,
a2, a3, … ?
𝑎 𝑛= 2𝑛 − 1
𝑛
𝑎 𝑛= ( − 1 )
𝑎 𝑛=𝑛 2+1
𝑎 𝑛= 0.25 𝑛
Geometric progression
Definition:
A geometric progression is a sequence of the form

where the initial term a and the common ratio r are real numbers.

Example:
 Let a = 1 and r = −1. Then:

 Let a = 2 and r = 5. Then:

 Let a = 6 and r = 1/3. Then:


Arithmetic progression
Definition:
A arithmetic progression is a sequence of the form

where the initial term a and the common difference d are real
numbers.

Example:
 Let a = −1 and d = 4. Then:

 Let a = 7 and d = −3. Then:

 Let a = 1 and d = 2. Then:


Strings

Definition: A string is a finite sequence of characters from a


finite set (an alphabet).
 The length of a string S is the number of terms that it consists
of.
 The empty string contains no terms at all. It has length zero
and is represented by λ.
 The string abcde has length 5.
Recurrence Relations
Definition: A recurrence relation for the sequence {an} is an equation that
expresses an in terms of one or more of the previous terms of the sequence,
namely, a0, a1, …, an-1, for all integers n with n ≥ n0, where n0 is a nonnegative
integer.

 A sequence is called a solution of a recurrence relation if its terms satisfy the


recurrence relation.

Example : Let {an} be a sequence that satisfies the recurrence relation for n =
1,2,3,4,…. and suppose that
a0 = 2 (initial condition). What are a1 , a2 and a3?
Solution:
a1 = a0 + 3 = 2 + 3 = 5
a2 = 5 + 3 = 8
a3 = 8 + 3 = 11
Fibonacci Sequence
Definition: The Fibonacci sequence, f0 ,f1 ,f2,…, is defined by:
O Initial Conditions: f0 = 0, f1 = 1
O Recurrence Relation:

Example: Find f2 ,f3 ,f4 , f5 and f6 .

Answer:
f2 = f1 + f0 = 1 + 0 = 1,
f3 = f2 + f1 = 1 + 1 = 2,
f4 = f3 + f2 = 2 + 1 = 3,
f5 = f4 + f3 = 3 + 2 = 5,
f6 = f5 + f4 = 5 + 3 = 8.
Solving Recurrence Relations
 Finding a formula for the nth term of the sequence generated by a recurrence
relation is called solving the recurrence relation.
 Such a formula is called a closed formula.
Example:
Let {an} be a sequence that satisfies the recurrence relation an = an-1 + 3 for n
= 2,3,4,…. and suppose that a1 = 2.
Method 1: Working upward, forward substitution
a2 = 2 + 3
a3 = (2 + 3) + 3 = 2 + 3 ∙ 2
a4 = (2 + 2 ∙ 3) + 3 = 2 + 3 ∙ 3
.
.
.
an = an-1 + 3 = (2 + 3 ∙ (n – 2)) + 3 = 2 + 3(n – 1)
Method 2: Working downward, backward substitution
an = an-1 + 3
= (an-2 + 3) + 3 = an-2 + 3 ∙ 2
= (an-3 + 3 )+ 3 ∙ 2 = an-3 + 3 ∙ 3
.
.
.

= a2 + 3(n – 2) = (a1 + 3) + 3(n – 2) = 2 + 3(n – 1)


Example:
How can we produce the terms of a sequence if the first 10 terms are
1, 3, 4, 7, 11, 18, 29, 47, 76, 123?

Solution:
The nth term will be
Where and .
Special Integer Sequences
O Given a few terms of a sequence, try to identify the sequence.
Conjecture a formula, recurrence relation, or some other rule.
O Some questions to ask?
O Are there repeated terms of the same value?
O Can you obtain a term from the previous term by adding an amount or
multiplying by an amount?
O Can you obtain a term by combining the previous terms in some way?
O Are they cycles among the terms?
O Do the terms match those of a well known sequence?
Example : Find formulae for the sequences with the
following first five terms: 1, ½, ¼, 1/8, 1/16
Solution: Note that the denominators are powers of 2.
The sequence with an = 1/2n is a possible match. This is a
geometric progression with a = 1 and r = ½.

Example : Consider 1,3,5,7,9


Solution: Note that each term is obtained by adding 2 to
the previous term. A possible formula is an = 2n + 1.
This is an arithmetic progression with a =1 and d = 2.
Useful Sequences
Summations
O To find the Sum of the terms from the sequence we use the
notation

or

O The variable j is called the index of summation. It runs through all


the integers starting with its lower limit m and ending at its
upper limit n.
O More generally for a set S:
Example :

 The sum of the first 1000 terms of the sequence {an} with an=n2
for n = 1, 2, 3, … is

 The sum of first n natural numbers can be represented as


Geometric Series
Theorem: If a and r are real numbers and , then

Proof:
Let
Geometric Series

If then

If then
Double Summations
 Double summations arise in various contexts for example nested
loops in C or Java.
Example:
Some Useful Summation Formulae
Example: Find

Solution:
Example: Compute the following summation

Solution:
Algorithm Examples

Example: an algorithm that finds the maximum element in


a finite sequence

Oprocedure max(a1, a2, …, an: integers)


Omax := a1
Ofor i := 2 to n
O if max < ai then max := ai
O{max is the largest element}
Algorithm Examples

OIf the terms in a sequence are ordered, a binary search


algorithm is more efficient than linear search.

OThe binary search algorithm iteratively restricts the


relevant search interval until it closes in on the position of
the element to be located.
Algorithm Examples

binary search for the letter ‘j’

search interval

a c d f g h j l m o p r s u v x z

center element
Algorithm Examples

binary search for the letter ‘j’

search interval

a c d f g h j l m o p r s u v x z

center element
Algorithm Examples

binary search for the letter ‘j’

search interval

a c d f g h j l m o p r s u v x z

center element
Algorithm Examples

binary search for the letter ‘j’

search interval

a c d f g h j l m o p r s u v x z

center element
Algorithm Examples

binary search for the letter ‘j’

search interval

a c d f g h j l m o p r s u v x z

center element

found !
Algorithm Examples
Oprocedure binary_search(x: integer; a1, a2, …, an:
integers)
Oi := 1 {i is left endpoint of search interval}
Oj := n {j is right endpoint of search interval}
Owhile (i < j)
Obegin
O m := (i + j)/2
O if x > am then i := m + 1
O else j := m
Oend
Oif x = ai then location := i
Oelse location := 0
O{location is the subscript of the term that equals x, or is
zero if x is not found}
Recursive algorithms

An algorithm is called recursive if it solves a problem by


reducing it to an instance of the same problem with smaller
input.

Example I: Recursive Euclidean Algorithm

Oprocedure gcd(a, b: nonnegative integers with a < b)


Oif a = 0 then gcd(a, b) := b
Oelse gcd(a, b) := gcd(b mod a, a)
Example II: Recursive Fibonacci Algorithm

Oprocedure fibo(n: nonnegative integer)


Oif n = 0 then fibo(0) := 0
Oelse if n = 1 then fibo(1) := 1
Oelse fibo(n) := fibo(n – 1) + fibo(n – 2)
ORecursive Fibonacci Evaluation:

f(4)

f(3) f(2)

f(2)

f(1) f(1) f(0)

f(1) f(0)
Oprocedure iterative_fibo(n: nonnegative integer)
Oif n = 0 then y := 0
Oelse
Obegin
O x := 0
O y := 1
O for i := 1 to n-1
O begin
O z := x + y
O x:=y
O y := z
O end
Oend {y is the n-th Fibonacci number}
OFor every recursive algorithm, there is an equivalent
iterative algorithm.

ORecursive algorithms are often shorter, more elegant,


and easier to understand than their iterative counterparts.

OHowever, iterative algorithms are usually more efficient


in their use of space and time.

You might also like