0% found this document useful (0 votes)
12 views66 pages

Ds 5 Function

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views66 pages

Ds 5 Function

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai

Chapter 5
Xuan Toan, Tran Hong
Tai

Functions
Discrete Structures for Computing on June 16, 2024

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

Nguyen An Khuong, Tran Tuan Anh, Mai Xuan Toan, Tran Hong
Tai
Faculty of Computer Science and Engineering
University of Technology - VNUHCM
[email protected]
5.1
Contents Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

1 Functions

Contents

2 One-to-one and Onto Functions Functions

One-to-one and Onto


Functions

Sequences and
Summation

3 Sequences and Summation Recursion

4 Recursion

5.2
Course outcomes Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Course learning outcomes


L.O.1 Understanding of logic and discrete structures
L.O.1.1  Describe definition of propositional and predicate logic
L.O.1.2  Define basic discrete structures: set, mapping, graphs

L.O.2 Represent and model practical problems with discrete structures Contents

L.O.2.1  Logically describe some problems arising in Computing Functions

L.O.2.2  Use proving methods: direct, contrapositive, induction One-to-one and Onto
Functions
L.O.2.3  Explain problem modeling using discrete structures
Sequences and
Summation

L.O.3 Understanding of basic probability and random variables Recursion


L.O.3.1  Define basic probability theory
L.O.3.2  Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1  Operate (compute/ optimize) on discrete structures
L.O.4.2  Compute probabilities of various events, conditional
ones, Bayes theorem

5.3
Introduction Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

• Each student is assigned a grade from set


{0, 0.1, 0.2, 0.3, . . . , 9.9, 10.0} at the end of semester Contents

• Function is extremely important in mathematics and


Functions

One-to-one and Onto


computer science Functions

• linear, polynomial, exponential, logarithmic,... Sequences and


Summation

• Don't worry! For discrete mathematics, we need to Recursion

understand functions at a basic set theoretic level

5.4
Function Functions

Nguyen An Khuong,

Definition Tran Tuan Anh, Mai


Xuan Toan, Tran Hong

function f
Tai
Let A and B be nonempty sets. A from A to B is an
assignment of exactly one element of B to each element of A.
• f :A→B
• A: domain (mi·n x¡c ành) of f
• B: codomain (mi·n gi¡ trà ) of f Contents

• For each a ∈ A, if f (a) = b Functions

• b is an image (£nh) of a One-to-one and Onto


Functions
• a is pre-image (nghàch £nh) of f (a) Sequences and

• Range of f is the set of all images of elements of A Summation

Recursion
• f maps (¡nh x¤) A to B

f
a b = f (a)

A B
f
5.5
Example Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

y is an image of d
c is a pre-image of z

5.6
Example Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Example
What are domain, codomain, and range of the function that
assigns grades to students includes: student A: 5, B: 3.5, C: 9, D:
5.2, E: 4.9?
Contents

Functions

Example One-to-one and Onto


Functions

f : Z → Z assign the the square of an integer


Let to this integer. Sequences and
Summation
What is f (x)? Domain, codomain, range of f ?
Recursion

• f (x) = x2
• Domain: set of all integers

• Codomain: Set of all integers

• Range of f : {0, 1, 4, 9, . . .}

5.7
Add and multiply real-valued functions Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Definition
Let f1 and f2 be functions from A to R. Then f1 + f2 and f1 f2
are also functions from A to R defined by

(f1 + f2 )(x) = f1 (x) + f2 (x) Contents

(f1 f2 )(x) = f1 (x)f2 (x) Functions

One-to-one and Onto


Functions

Sequences and

Example Summation

Recursion

Let f1 (x) = x2 and f2 (x) = x − x2 . What are the functions


f1 + f2 and f1 f2 ?
(f1 + f2 )(x) = f1 (x) + f2 (x) = x2 + x − x2 = x
(f1 f2 )(x) = f1 (x)f2 (x) = x2 (x − x2 ) = x3 − x4

5.8
Image of a subset Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong

Definition
Tai

Let f : A → B and S ⊆ A. The image of S:

f (S) = {f (s) | s ∈ S}

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

f ({a, b, c, d}) = {x, y, z}

5.9
One-to-one Functions

Nguyen An Khuong,

Definition Tran Tuan Anh, Mai


Xuan Toan, Tran Hong

ìn ¡nh)
Tai

A function f is one-to-one or injective ( if and only if

∀a∀b (f (a) = f (b) → a = b)

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
• Is f : Z → Z, f (x) = x + 1
one-to-one?

• Is f : Z → Z, f (x) = x2
one-to-one?

5.10
Onto Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai

Definition Xuan Toan, Tran Hong


Tai

f :A→B to n ¡nh)
is onto or surjective ( if and only if

∀b ∈ B, ∃a ∈ A : f (a) = b

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
• Is f : Z → Z, f (x) = x + 1
onto?

• Is f : Z → Z, f (x) = x2
onto?

5.11
One-to-one and onto (bijection) Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Definition
f :A→B is bijective (one-to-one correspondence) ( song ¡nh) if
and only if f is injective and surjective Contents

Functions

One-to-one and Onto


Functions
• Let f be the function from Sequences and

{1, 2, 3, 4} to {D, B, C, A} Summation

with f (1) = D , f (2) = B , Recursion

f (3) = C , f (4) = A.
Is f a bijection?

5.12
Example Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.13
Inverse function (H m ng֖c) Functions

Nguyen An Khuong,

Definition Tran Tuan Anh, Mai


Xuan Toan, Tran Hong
Tai
Let f : A → B be a bijection then the inverse of f is the function
f −1 : B → A defined by

if f (a) = b then f −1 (b) = a

A one-to-one correspondence is call invertible (kh£ nghàch) Contents

because we can define the inverse of this function. Functions

One-to-one and Onto


Functions

f (a) Sequences and


Summation

Recursion

−1
a = f −1 (b) f (b) b = f (a)

A f B

f −1
5.14
Example Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Example
A = {a, b, c} and B = {1, 2, 3} with

f (a) = 2 f (b) = 3 f (c) = 1 Contents

Functions

f is invertible and its inverse is


One-to-one and Onto
Functions

Sequences and

f −1 (1) = c f −1 (2) = a f −1 (3) = b Summation

Recursion

Example
Let f :R→R with f (x) = x2 . If f invertible?

5.15
Example Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

f :R→R
Contents

Functions
f (x) = 2x + 1
One-to-one and Onto
Functions

Sequences and

f −1 : R → R Summation

Recursion

x−1
f −1 (x) =
2

5.16
Function Composition Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Definition
Given a pair of functions g : A → B and f : B → C . Then the Contents

composition (hñp th nh) of f and g , denoted f ◦ g is defined by Functions

One-to-one and Onto


Functions
f ◦g :A→C
Sequences and
Summation

f ◦ g(a) = f (g(a)) Recursion

5.17
Example Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.18
Graphs of Functions Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Example
The graph of f (x) = x2 from Z to Z.

(−3, 9) (3, 9)
Contents

Functions

(−2, 4) (2, 4) One-to-one and Onto


Functions

Sequences and
Summation
(−1, 1) (1, 1)
Recursion

(0, 0)

Definition
Let f be a function from the set A to the set B . The graph of the
function f is the set of ordered pairs {(a, b) | a ∈ A and f (a) = b}.

5.19
Important Functions Functions

Nguyen An Khuong,

Definition Tran Tuan Anh, Mai


Xuan Toan, Tran Hong

h m s n) of x (⌊x⌋): the largest integer ≤ x


Tai

Floor function (
⌊ 12 ⌋ = 0, ⌊3.1⌋ = 3, ⌊7⌋ = 7
Ceiling function (h m tr¦n) of x (⌈x⌉): the smallest integer ≥ x
⌈ 12 ⌉ = 1, ⌈3.1⌉ = 4, ⌈7⌉ = 7
Contents

B£ng: Properties (n is an integer, x is a real number)


Functions

One-to-one and Onto


Functions

(1a) ⌊x⌋ = n iff n≤x<n+1 Sequences and


Summation
(1b) ⌈x⌉ = n iff n−1<x≤n
Recursion
(1c) ⌊x⌋ = n iff x−1<n≤x
(1d) ⌈x⌉ = n iff x≤n<x+1
(2) x − 1 < ⌊x⌋ ≤ ⌈x⌉ < x + 1
(3a) ⌊−x⌋ = −⌈x⌉
(3b) ⌈−x⌉ = −⌊x⌋
(4a) ⌊x + n⌋ = ⌊x⌋ + n
(4b) ⌈x + n⌉ = ⌈x⌉ + n
5.20
Sequences Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

What are the rule of these sequences ( d¢y )?


Example
1, 3, 5, 7, 9, . . . an = 2n − 1
Arithmetic sequence ( c§p sè cëng )
Contents

Functions

One-to-one and Onto


Example Functions

Sequences and
1 1 1 1 1
1, 2 , 4 , 8 , 16 , . . . an = 2n−1 Summation

Geometric sequence (c§p sè nh¥n) Recursion

Example
{an } 5, 11, 17, 23, 29, 35, 41, 47, ... an = 6n − 1
{bn } 1, 7, 25, 79, 241, 727, 2185, ... bn = 3n − 2

5.21
Recurrence Relations Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai

Example Xuan Toan, Tran Hong


Tai

{an } 5, 11, 17, 23, 29, 35, 41, 47, . . .


an = an−1 + 6 for n = 2, 3, 4, . . . and a1 = 5
Recurrence relations: cæng thùc truy hçi

Contents

Definition (Fibonacci Sequence) Functions

One-to-one and Onto


f0 = 0 and f1 = 1
Initial condition: Functions

fn = fn−1 + fn−2 for n = 2, 3, 4, . . . Sequences and


Summation

Recursion

Example
Find the Fibonacci numbers f2 , f3 , f4 , f5 and f6
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
5.22
Functions

Exercise (1) Nguyen An Khuong,


Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Initial deposit: $10,000 Tai

Interest: 11%/year, compounded annually ( l¢i su§t k²p )


After 30 years, how much do you have in your account?

Solution:
Let Pn be the amount in the account after n years. The sequence
Contents
{Pn } satisfies the recurrence relation
Functions
Pn = Pn−1 + 0.11Pn−1 = (1.11)Pn−1 . One-to-one and Onto

The initial condition is P0 = 10, 000 Functions

Sequences and

Step 1. Solve the recurrence relation (iteration technique) Summation

Recursion
P1 = (1.11)P0
P2 = (1.11)P1 = (1.11)2 P0
P3 = (1.11)P2 = (1.11)3 P0
.
.
.
Pn = (1.11)Pn−1 = (1.11)n P0 .
Step 2. Calculate
P30 = (1.11)30 10, 000 = $228, 922.97.
5.23
Functions

Nguyen An Khuong,

Exercise (2) Tran Tuan Anh, Mai


Xuan Toan, Tran Hong
Tai

What is the 2012th number in the sequence {xn }: 1, 2, 2, 3, 3, 3,


4, 4, 4, 4, 5, 5, 5, 5, 5, 6,. . .

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.24
Functions

Nguyen An Khuong,

Exercise (2) Tran Tuan Anh, Mai


Xuan Toan, Tran Hong
Tai

What is the 2012th number in the sequence {xn }: 1, 2, 2, 3, 3, 3,


4, 4, 4, 4, 5, 5, 5, 5, 5, 6,. . .

Solution:
In this sequence, integer 1 appears once, the integer 2 appears
twice, the integer 3 appears three times, and so on. Therefore Contents

integer n appears n times in the sequence. Functions

One-to-one and Onto


We can prove that (try it!) Functions

n
X n(n + 1) Sequences and

i = 1 + 2 + 3 + ... + n = Summation

i=1
2 Recursion

and can easily calculate that


62
X
i = 1953
i=1
so the next 63 numbers (until 2016) is 63.

Therefore, 2012th number in the sequence is 63.

5.24
Functions

Theorem Nguyen An Khuong,


Tran Tuan Anh, Mai

If a and r are real numbers and r ̸= 0, then Xuan Toan, Tran Hong
Tai

n  ar n+1
−a
X if r≠ 1
arj = r−1
j=0
(n + 1)a if r = 1.

Chùng minh.
Contents

Functions
Pn j
Let Sn = j=0 ar .
One-to-one and Onto
Functions

Pn Sequences and

rSn = r j=0 arj Summation


Pn
= j=0 arj+1 Recursion

Pn+1
=  k=1 ark 
Pn k
= k=0 ar + (arn+1 − a)
= Sn + (arn+1 − a)

ar n+1 −a
Solving for Sn shows that if r ̸= 1, then Sn = r−1
Pn
If r = 1, then Sn = j=0 a = (n + 1)a

5.25
Recursion Functions

Nguyen An Khuong,

Definition (Recurrence Relation)


Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

An equation that recursively defines a sequence.

Definition (Recursion (» quy))


The act of defining an object (usually a function) in terms of that
Contents

object itself. Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.26
Recursive Algorithms Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Definition
An algorithm is called recursive if it solves a problem by reducing
it to an instance of the same problem with smaller input.

Example
Contents

Functions

Give a recursive algorithm for computing n!, where n is a One-to-one and Onto
Functions
nonnegative integer.
Sequences and
Summation

Solution. We base on the recursive definition of n!: Recursion

n! = n · (n − 1)! and 0! = 1.
procedure factorial (n: nonnegative integer)
if n = 0 then return 1
else return n· factorial (n - 1)
{output is n!}

5.27
Algorithms for Fibonacci Numbers Functions

Nguyen An Khuong,

Recursive Algorithm Tran Tuan Anh, Mai


Xuan Toan, Tran Hong
Tai

procedure fibonacci (n: nonnegative integer)


if n = 0 then return 0
else if n = 1 then return 1
else return fibonacci (n-1) + fibonacci (n-2)
{output is fibonacci (n)}
Contents

Functions
Iterative Algorithm One-to-one and Onto

procedure iterative fibonacci (n:


Functions
nonnegative integer)
Sequences and

if n = 0 then return 0 Summation

else Recursion

x := 0
y := 1
for i := 1 to n − 1
z := x + y
x := y
y := z
return y
{output is the nth Fibonacci number}
5.28
Tower of Hanoi Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

There is a tower in Hanoi that has three pegs mounted on a board


together with 64 gold disks of different sizes.

Initially, these disks are placed on the first peg in order of size,
with the largest on the borrom. Contents

Functions

The rules: One-to-one and Onto


Functions
1 Move one at a time from one peg to another
Sequences and
Summation
2 A disk is never placed on top of a smaller disk
Recursion

Goals: all the disks on the third peg in order of size.

The myth says that the world will end when they finish the
puzzle.

5.29
Tower of Hanoi  64 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

?
1
Contents
2
Functions
3
One-to-one and Onto
Functions
4
Sequences and

5 Summation

Recursion
6

5.30
Tower of Hanoi  1 Disc Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
1

5.31
Tower of Hanoi  1 Disc Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
1

Moved disc from peg 1 to peg 3.

5.32
Tower of Hanoi  1 Disc Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

OK Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
1

5.33
Tower of Hanoi  2 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
2

5.34
Tower of Hanoi  2 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
2 1

Moved disc from peg 1 to peg 2.

5.35
Tower of Hanoi  2 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
1 2

Moved disc from peg 1 to peg 3.

5.36
Tower of Hanoi  2 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
2

Moved disc from peg 2 to peg 3.

5.37
Tower of Hanoi  2 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

OK Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
2

5.38
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions
1
Sequences and

2 Summation

Recursion
3

5.39
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

2 Summation

Recursion
3 1

Moved disc from peg 1 to peg 3.

5.40
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
3 2 1

Moved disc from peg 1 to peg 2.

5.41
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
3 2

Moved disc from peg 3 to peg 2.

5.42
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
2 3

Moved disc from peg 1 to peg 3.

5.43
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion
1 2 3

Moved disc from peg 2 to peg 1.

5.44
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

2 Summation

Recursion
1 3

Moved disc from peg 2 to peg 3.

5.45
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions
1
Sequences and

2 Summation

Recursion
3

Moved disc from peg 1 to peg 3.

5.46
Tower of Hanoi  3 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

OK 1
Contents

Functions

One-to-one and Onto


Functions

Sequences and

2 Summation

Recursion
3

5.47
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions
1
One-to-one and Onto
Functions
2
Sequences and

3 Summation

Recursion
4

5.48
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions
2
Sequences and

3 Summation

Recursion
4 1

Moved disc from peg 1 to peg 2.

5.49
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

3 Summation

Recursion
4 1 2

Moved disc from peg 1 to peg 3.

5.50
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

3 1 Summation

Recursion
4 2

Moved disc from peg 2 to peg 3.

5.51
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
4 3 2

Moved disc from peg 1 to peg 2.

5.52
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
4 3 2

Moved disc from peg 3 to peg 1.

5.53
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 2 Summation

Recursion
4 3

Moved disc from peg 3 to peg 2.

5.54
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions
1
Sequences and

2 Summation

Recursion
4 3

Moved disc from peg 1 to peg 2.

5.55
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions
1
Sequences and

2 Summation

Recursion
3 4

Moved disc from peg 1 to peg 3.

5.56
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

2 1 Summation

Recursion
3 4

Moved disc from peg 2 to peg 3.

5.57
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
2 3 4

Moved disc from peg 2 to peg 1.

5.58
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 Summation

Recursion
2 3 4

Moved disc from peg 3 to peg 1.

5.59
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

1 3 Summation

Recursion
2 4

Moved disc from peg 2 to peg 3.

5.60
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions

Sequences and

3 Summation

Recursion
2 1 4

Moved disc from peg 1 to peg 2.

5.61
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions

One-to-one and Onto


Functions
2
Sequences and

3 Summation

Recursion
1 4

Moved disc from peg 1 to peg 3.

5.62
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

Contents

Functions
1
One-to-one and Onto
Functions
2
Sequences and

3 Summation

Recursion
4

Moved disc from peg 2 to peg 3.

5.63
Tower of Hanoi  4 Discs Functions

Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai

OK 1

2
Contents

Functions

One-to-one and Onto


Functions

Sequences and

3 Summation

Recursion
4

5.64
Tower of Hanoi Functions

Nguyen An Khuong,

Algorithm Tran Tuan Anh, Mai


Xuan Toan, Tran Hong

procedure hanoi (n, A, B, C)


Tai

if n = 1 then move the disk from A to C


else
call hanoi (n − 1, A, C, B)
move disk n from A to C
call hanoi (n − 1, B, A, C) Contents

Functions

One-to-one and Onto

Recurrence Relation Functions

Sequences and
 Summation

1 if n=1 Recursion
H(n) =
2H(n − 1) + 1 if n > 1.

Recurrence Solving
H(n) = 2n − 1
If one move takes 1 second, for n = 64

264 − 1 ≈ 2 × 1019 sec


≈ 500 billion years!.
5.65

You might also like