Вы находитесь на странице: 1из 16

1

Recursive Functions
and Mathematical Induction
by Axel Boldt and Michael Green
(revised 2/20/2008)


Recursive Functions

Suppose you put $10,000 in a bank account that pays interest of 3% compounded
annually. This means that at the end of one year the bank will compute 3% of your
$10,000 and add that amount to your account. So at the end of the first year you will
have 10,000 + 0.03(10,000) = 1.03(10,000) = 10,300 dollars in your account. The next
year you will earn interest on this new amount. So at the end of the second year you will
have 10,300 + 0.03(10,300) = 1.03(10,300) = 10,609 dollars. This process continues into
the third year, fourth year, etc....

Let f(n) denote the amount in the bank account after n years and f(n 1) the amount after
n 1 years. The fact that we start with $10,000 in the account is written as f(0) = 10,000.
Knowing that the amount after n years is 3% more than the amount after n 1 years gives
the relation ) 1 ( 03 . 1 ) ( = n f n f .

Together, these two relations

=
=
1 for ) 1 ( 03 . 1 ) (
000 , 10 ) 0 (
n n f n f
f


are enough to determine the value of f(n) for any integer value of 0 n . For example, to
find the value of f(3), the amount in the bank account after 3 years, we compute

300 , 10 ) 000 , 10 ( 03 . 1 ) 0 ( 03 . 1 ) 1 (
) 1 ( 03 . 1 ) 2 (
) 2 ( 03 . 1 ) 3 (
= = =
=
=
f f
f f
f f


now substituting the value of f(1) into the 2
nd
equation gives

609 , 10 ) 300 , 10 ( 03 . 1 ) 2 ( = = f

and finally substituting the value of f(2) into the first equation gives

27 . 927 , 10 ) 609 , 10 ( 03 . 1 ) 3 ( = = f

So, the amount in the bank account after 3 years is $10,927.27
2

The relations

=
=
1 for ) 1 ( 03 . 1 ) (
1000 ) 0 (
n n f n f
f


are said to define the function f recursively.

In general, a function f is defined recursively if its definition refers to itself. In the above
example, the line ) 1 ( 03 . 1 ) ( = n f n f defines f in terms of itself. Using this relation over
and over again will eventually lead us to f(0), a known value of the function because of
the line f(0) = 10,000 (which is called the base case).

Many computer languages allow you to define functions recursively.



Example. Suppose the function g is defined recursively as

+ =
=
2 for 1 ) 1 ( 2 ) (
3 ) 1 (
n n g n g
g


To compute g(4) we proceed as follows:

7 1 3 2 1 ) 1 ( 2 ) 2 (
1 ) 2 ( 2 ) 3 (
1 ) 3 ( 2 ) 4 (
= + = + =
+ =
+ =
g g
g g
g g

substituting back into g(3):
15 1 7 2 ) 3 ( = + = g
substituting back into g(4):
31 1 15 2 ) 4 ( = + = g
so g(4) = 31.

In principle, g(n) can be computed for every positive integer n in the same fashion. This
method of finding g(n) is called a top down approach. Once we get to the value of g(1)
the process of substituting values back into the previous expressions is called back
substitution.







3
Example. The Fibonacci Numbers are a famous example of a recursively defined
sequence of numbers. They are defined as

+ =
=
=
2 for ) 2 ( ) 1 ( ) (
1 ) 1 (
0 ) 0 (
n n f n f n f
f
f



To calculate f(5) we write:
1 0 1 ) 0 ( ) 1 ( ) 2 (
) 1 ( ) 2 ( ) 3 (
) 2 ( ) 3 ( ) 4 (
) 3 ( ) 4 ( ) 5 (
= + = + =
+ =
+ =
+ =
f f f
f f f
f f f
f f f

now, substituting backwards
5 2 3 ) 5 (
3 1 2 ) 4 (
2 1 1 ) 3 (
= + =
= + =
= + =
f
f
f

so f(5) = 5.

Alternatively, the Fibonacci Numbers can be computed from the bottom up by starting
with the base cases:
n f(n)
0 0
1 1
2 0 + 1 = 1
3 1 + 1 = 2
4 1 + 2 = 3
5 2 + 3 = 5
6 3 + 5 = 8
7 5 + 8 = 13
8 8 + 13 = 21
9 13 + 21 = 34


Problem. How many bit strings of length 6 do not contain 2 consecutive 0s? Develop a
method to answer this question for bit strings of any length.

Solution. We begin by answering the more general problem for bit strings of length n.
Let

a(n) = # of bit strings of length n that do not contain 2 consecutive 0s.

4
Assuming for the moment that 3 n , we can divide the bit strings into two groups: those
ending in 1 and those ending in 0. Note, however, that those ending in a 0 must have a 1
as the next to last digit (otherwise there would be two consecutive zeros). Also, the
preceding digits must make up a shorter bit string that also doesnt have any consecutive
zeros. With this in mind we can count how many bit strings are possible in these two
groups:

Those ending in 1: ______n - 1 bits__________ 1 there are a(n 1) of these.
Those ending in 0: _____ n - 2 bits_________1 0 there are a(n 2) of these.

Since together these two cases make up all the possible bit strings of length n, the total is
a(n) = a(n 1) + a(n 2).

This relates our problem (finding a(n)) with two simpler problems (finding a(n 1) and
a(n 2)). We need to find values for a(1) and a(2) in order to complete the recursive
definition. For these cases we can just list all the possibilities:
a(1) = # of bit strings of length 1 that do not have 2 consecutive 0s = #{0, 1} = 2
a(2) = # of bit strings of length 2 that do not have 2 consecutive 0s = #{10, 01, 11} = 3.
So a(n) is defined recursively by

+ =
=
=
) 2 ( ) 1 ( ) (
3 ) 2 (
2 ) 1 (
n a n a n a
a
a


We can now answer the question: what is a(6)?

5 2 3 ) 1 ( ) 2 ( ) 3 (
) 2 ( ) 3 ( ) 4 (
) 3 ( ) 4 ( ) 5 (
) 4 ( ) 5 ( ) 6 (
= + = + =
+ =
+ =
+ =
a a a
a a a
a a a
a a a

and substituting backwards
21 8 13 ) 6 (
13 5 8 ) 5 (
8 3 5 ) 4 (
= + =
= + =
= + =
a
a
a

so there are 21 bit strings of length 6 that do not have two consecutive 0s.


Summation Notation

Summation notation was developed to make it easier to work with large sums. Rather
than write the sum of the first 20 positive integers as

20 19 18 17 16 15 14 13 12 11 10 7 6 5 4 3 2 1 + + + + + + + + + + + + + + + + +

5
the same sum is written in summation notation as

=
20
1 i
i . The notation means to start with
a value of i = 1, then increment i by 1 until you get to 20. When you are done you should
add everything together.

The sum of the first 5 positive integers is 15 5 4 3 2 1
5
1
= + + + + =

= i
i

If you want the sum of the first 5 positive even integers you can write

=
= + + + + =
5
1
30 ) 5 ( 2 ) 4 ( 2 ) 3 ( 2 ) 2 ( 2 ) 1 ( 2 2
i
i
That is, you start with i = 1, plug that into the expression 2i to get 2(1), then increment i
by 1 to get i = 2, plug that into the expression 2i to get 2(2), and so on until you get to i =
5. When you are done you add all the terms together.

The sum of the first 5 positive odd integers can be written as
25 9 7 5 3 1 ] 1 ) 5 ( 2 [ ] 1 ) 4 ( 2 [ ] 1 ) 3 ( 2 [ ] 1 ) 2 ( 2 [ ] 1 ) 1 ( 2 [ ) 1 2 (
5
1
= + + + + = + + + + =

= i
i

You dont have to start with i = 1. If you want the sum of terms of the form i/2 where i
goes from 5 to 10 just write:
2 / 10 2 / 9 2 / 8 2 / 7 2 / 6 2 / 5 2 /
10
5
+ + + + + =

= i
i .

Heres another, more complicated looking sum.
38 18 12 8 ] 8 ) 2 ( ) 2 ( 3 [ ] 8 ) 1 ( ) 1 ( 3 [ ] 8 ) 0 ( ) 0 ( 3 [ ] 8 3 [
2
0
2 2 2 2
= + + = + + + + + + + + = + +

= i
i i

The variable i is called a dummy variable. It doesnt appear in the final answer and is
only there to show the form of the individual terms in the sum. Because of this, we dont
need to use the letter i, we can use any other letter if we want.

15 5 4 3 2 1
5
1
= + + + + =

= k
k

=
= + + + = + + + =
4
1
16 7 5 3 1 ] 1 ) 4 ( 2 [ ] 1 ) 3 ( 2 [ ] 1 ) 2 ( 2 [ ] 1 ) 1 ( 2 [ ) 1 2 (
t
t

Summations are often used in computer programming. The sum can be easily carried out
using a for loop as follows. The sum ) 1 2 (
5
1

i
i is written in pseudocode as

6
sum := 0
for i = 1 to 5
sum := sum + (2i 1)

In Java this looks like

int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + (2*i - 1);
}

In the program above, the final value of the sum will be the value of the variable sum
after the for loop executes.

It is very common to have a large sum where you do not know the number of terms you
are summing up. For example, you might want to add the first n positive integers
together, but you dont know what n is. This is written as
n i
n
i
+ + + + =

=
L 3 2 1
1
.

The three dots mean to continue the same pattern until you get to n. The sum of the first
n positive odd integers would be written as
) 1 2 ( 5 3 1 ) 1 2 (
1
+ + + + =

=
n i
n
i
L

Complicated sums can often be simplified using the distributive law. This distributive
law says that ay ax y x a + = + ) ( . That also works with larger sums:
n n
ax ax ax x x x a + + + = + + + L L
2 1 2 1
) ( .
Written in summation notation, this gives the rule:

= =
=
n
i
i
n
i
i
ax x a
1 1

where a is a term that doesnt involve the dummy variable i.
So, for example, 30 ) 15 ( 2 ) 5 4 3 2 1 ( 2 2 2
5
1
5
1
= = + + + + = =

= = i i
i i .

Matrix multiplication is easily written in summation notation:
|
|
|
|
|

\
|
=
|
|
|
|
|

\
|
|
|
|
|
|

\
|
mp m m
p
p
np n n
p
p
mn m m
n
n
c c c
c c c
c c c
b b b
b b b
b b b
a a a
a a a
a a a
L
M O M M
L
L
L
M O M M
L
L
L
M O M M
L
L
2 1
2 22 21
1 12 11
2 1
2 22 21
1 12 11
2 1
2 22 21
1 12 11

where

=
=
n
k
kj ik ij
b a c
1
.
7
For example, this says that

=
=
n
k
k k
b a c
1
1 2 21
. You should check to make sure this
corresponds to taking the 2
nd
row of the first matrix times the 1
st
column of the second
matrix.


Summations can be written recursively. Consider the function

=
=
n
i
i n f
1
) ( . That is, f(n)
is the sum of the first n positive integers. We can define f recursively as

+ =
=
2 for ) 1 ( ) (
1 ) 1 (
n n n f n f
f

Convince yourself of this by writing out f(2), f(3), and f(4).



Products

What if instead of adding the first n positive integers together you want to multiply them
all together? There is a special notation for that as well: product notation.

n i
n
i
=

=
L 3 2 1
1


For example, the product of the first 4 positive odd integers is
105 7 5 3 1 ] 1 ) 4 ( 2 [ ] 1 ) 3 ( 2 [ ] 1 ) 2 ( 2 [ ] 1 ) 1 ( 2 [ ) 1 2 (
4
1
= = =

= i
i

The product of the first n positive integers shows up in so many places that we have a
special notation for it: n!. That is,
n n = L 3 2 1 ! .
The symbol n! is read as n factorial. So, for example, 120 5 4 3 2 1 ! 5 = = . It is
useful to also define 0! = 1.


Mathematical Induction

If P(n) is a statement form which depends on a positive integer n and we want to prove
that P(n) is true for all positive integers n, we can use the following 2-step method which
is called mathematical induction:

Step 1. (Base case) Show that P(1) is true.
Step 2. (Induction step) Show that P(n) implies P(n + 1) for every positive integer n.

8
If step 1 and step 2 are completed then it follows that P(n) is true for every positive
integer n. Here is the logic: step 1 says that P(1) is true. Now, letting n = 1 in step 2 (the
induction step) we can conclude that P(2) is true. (This is Modus Ponens:
) 2 ( )) 2 ( ) 1 ( ( ) 1 ( P P P P ). Now, knowing P(2) is true, we can let n = 2 in the
induction step and conclude that P(3) is true. By repeatedly applying the induction step
we can conclude that P(4), P(5), P(6), etc... are all true. Hence we conclude that P(n) is
true for all positive integers n.

If we want to show P(n) is true for all integers 0 n , we need to change step 1 to: Show
that P(0) is true and we need to change step 2 to Show that ) (n P implies ) 1 ( + n P for
every 0 n . Similarly, if we only want to show P(n) for all integers 5 n we can
change step 1 to: Show that P(5) is true and in step 2 we only need to consider 5 n .

As an analogy, suppose you want to build a machine that can climb steps. The machine
really only needs to be able to complete two tasks:

Task 1. Get on the first step.
Task 2. Move from one step to the next higher step.

If the machine is capable of completing task 1 and task 2, you can be sure the machine
can climb a long series of steps. Task 1 gets the machine on the steps, and then
repeatedly applying task 2 moves the machine higher and higher on the steps. It is the
same idea with mathematical induction.

Example. Induction is often used to prove that observed patterns continue indefinitely.
For example consider the sum of the first n odd integers:

n = 1: 1
n = 2: 1 + 3 = 4
n = 3: 1 + 3 + 5 = 9
n = 4: 1 + 3 + 5 + 7 = 16
n = 5: 1 + 3 + 5 + 7 + 9 = 25

Do you notice the pattern? It looks as though the sum of the first n odd integers is equal
to n
2
. While the above list is evidence that the sum is equal to n
2
, it is not a proof. It is
possible that the pattern works for some values of n, including those we have checked,
but fails to work for others. To be sure the pattern continues for all values of n (at least
where n is a positive integer) we need a proof. For that, we use induction. First, lets
write down exactly what we want to prove. The sum of the first n odd integers is

=
= + + + +
n
i
i n
1
) 1 2 ( ) 1 2 ( 5 3 1 L , and so we want to prove that the following statement
P(n):
2
1
) 1 2 ( n i
n
i
=

=

is true for all positive integers n.
9
Step 1. We need to show P(1) is true. P(1) is the statement
2
1
1
1 ) 1 2 ( =

= i
i . Both sides
of this equation are equal to 1, and so P(1) is true.

Step 2. Now we fix an integer 1 n and assume that P(n) is true. We want to show this
implies P(n + 1) is true. That is, we are assuming
2
1
) 1 2 ( n i
n
i
=

=
and want to show
2
1
1
) 1 ( ) 1 2 ( + =

+
=
n i
n
i
. We do this by starting with the left-hand side of P(n + 1) and try to
get the right-hand side:
2
2
2
1
1
1
) 1 (
1 2
true is ) ( that assumption the using ) 1 ) 1 ( 2 (
) 1 ) 1 ( 2 ( ) 1 2 ( ) 1 2 (
+ =
+ + =
+ + =
+ +
(

=

+
+
=
n
n n
n P n n
n i i
n
i
n
i

Be sure you understand the first step. We are rewriting the sum of all n + 1 terms as the
sum of the first n terms plus the (n + 1)
st
term. We finally end up showing that the sum
of the first n + 1 odd integers is indeed equal to
2
) 1 ( + n which is what we wanted to
show in step 2. Since step 1 and step 2 have been completed, we have shown that P(n) is
true for all 1 n . QED



Example. Let x be a real number not equal to 1. Show that
x
x
x
n n
i
i

=
+
=

1
1
1
0
for all
integers 1 n .

Proof. We will use mathematical induction. Here P(n) is the statement:
x
x
x
n n
i
i

=
+
=

1
1
1
0
.
Step 1: We need to show that P(1) is true, that is

=
+

=
1
0
1 1
1
1
i
i
x
x
x . We will look at both
sides of this expression separately to see that they are equal. The left-hand side is

=
+ = + =
1
0
1 0
1
i
i
x x x x . The right-hand side is x
x
x x
x
x
x
x
+ =

+
=

+
1
1
) 1 )( 1 (
1
1
1
1
2 1 1
.
So we see that, when n = 1, both sides of the expression are equal to 1 + x. So P(1) is
true.

Step 2: Now fix an arbitrary integer 1 n and assume that P(n) is true. For this
particular value of n we will now try to show this implies P(n + 1) is true. That is, we are
10
assuming
x
x
x
n n
i
i

=
+
=

1
1
1
0
and we are trying to show
x
x
x
n n
i
i

=
+ + +
=

1
1
1 ) 1 ( 1
0
. We do this as
follows:
x
x
x
x x
x
x
x
x x
x
x
x
x
x
x x x
n
n n n
n n
n
n
n
n
i
i
n
i
i

=
+

=
+ |

\
|
=
+
+ + +
+ +
+
+
+
=
+
=

1
1
1 1
1
1
) 1 (
1
1
1
1
) (
2
2 1 1
1 1
1
1
1
1
1
1

Notice that this last expression is what we were trying to show. In the first equality, we
break the sum up into the first n summands, followed by the (n + 1)
st
summand. In the
second equality, we use our induction assumption. After that, it is algebra. This
completes step 2 of the induction proof. Hence the formula must be true for all positive
integers n. QED



Example. Show that every set with n elements has
n
2 subsets.

Proof. Step 1. The smallest set is the empty set with 0 = n elements. It has only one
subset, namely itself. This agrees with the formula 1 2
0
= .

Step 2. Fix an arbitrary number 0 n and assume that every set with n elements has
n
2
subsets. Let S be some set with 1 + n elements. We need to prove that S has
1
2
+ n
subsets.
In order to do this, distinguish one element x of S (this is possible since 0 1 > + n ). Every
subset of S either contains x or it doesnt; we will show that there are precisely as many
subsets that contain x as there are subsets that dont contain x. This can be done by
exhibiting a one-to-one correspondence between those two classes of subsets. If a subset
contains x, then by removing x, one gets a subset that doesnt contain x; conversely, if a
subset doesnt contain x, then by adding x to it, one gets a subset that contains x. These
two operations are inverse to each other, and that establishes that there are just as many
subsets of S which contain x as there are subsets which dont contain x. But how many?
The subsets of S that dont contain x are precisely the subsets of the set } {x S , and this
set has n elements (since S has 1 + n ). By our induction assumption, we know therefore
that } {x S has
n
2 different subsets. Overall we have therefore
n n
2 2 + subsets of S, and
1
2 2 2 2 2
+
= = +
n n n n
which is what we needed to prove. QED


11
Example. Show that n
n
2 2 for all positive integers n.

Proof. Step 1. The case n = 1. 2 2 2
1
= is true.
Step 2. We now assume that n
n
2 2 is true for some positive integer n. We need to
show that ) 1 ( 2 2
1
+
+
n
n
. We have ) 1 ( 2 ) ( 2 2 2 2 2 2
1
+ + = =
+
n n n n
n n
. This
completes step 2. QED



Example. Consider the following recursive function:

+ =
=

2 for ) 1 ( 2 ) (
1 ) 1 (
1
n n f n f
f
n

Show that 1 2 ) ( =
n
n f for all positive integers n. (This is called a closed or
explicit formula for f(n).)

Proof. We set 1 2 ) ( =
n
n g and show ) ( ) ( n g n f = for all 1 n by mathematical
induction.
Step 1. 1 1 2 ) 1 (
1
= = g and 1 ) 1 ( = f as well.
Step 2. Assume that ) ( ) ( n g n f = for some positive integer n. We need to show that
: ) 1 ( ) 1 ( + = + n g n f
). 1 (
1 2
1 2 2
1 2 2
assumption induction the of because ) ( 2
of instead 1 with , definition recursive the using ) ( 2 ) 1 (
1
+ =
=
=
+ =
+ =
+ + = +
+
n g
n g
n n n f n f
n
n
n n
n
n

QED


Sometimes, induction proofs work better if, in order to prove P(n + 1), we can refer to all
the previous cases P(1), P(2),, P(n). This is called the strong principle of
mathematical induction:

Step 1. (Base case) Show that P(1) is true.
*

Step 2. (Induction step) Show that P(1), P(2),, P(n) imply P(n + 1) for every positive
integer n.

Also, it is not necessary to start with the base case n = 1; n = 0 works just as well.


*
It is sometimes necessary to show more than just the single base case P(1). We will see how that can
happen in the last example of these notes.
12

Example. Exponentiation by Squaring. Consider the function:

=
=
=
odd is if )) 2 / ) 1 (( ( ) (
even and zero an greater th is if )) 2 / ( ( ) (
1 ) 0 (
2
2
n n pow x n pow
n n pow n pow
pow

We claim that
n
x n pow = ) ( for every integer 0 n . We can prove this using the strong
principle of induction with base case 0.

Proof. Step 1. 1 ) 0 ( = pow and 1
0
= x , which means that the case 0 = n is settled.
Step 2. Assume that
k
x k pow = ) ( for n k ..., , 1 , 0 = . We need to show that
1
) 1 (
+
= +
n
x n pow . We distinguish two cases:
(1) n is odd. Then n + 1 is even, and the definition of the pow function, together with the
induction hypothesis, yields:
1
2 / ) 1 ( 2
2 2 / ) 1 (
2
) (
)) 2 / ) 1 (( ( ) 1 (
+
+
+
=
=
=
+ = +
n
n
n
x
x
x
n pow n pow

(2) n is even. Then n + 1 is odd, and we get:
1
1 2 / 2
2 2 /
2
2
) (
)) 2 / ( (
)) 2 / ) 1 1 (( ) 1 (
+
+
=
=
=
=
+ = +
n
n
n
x
x
x x
n pow x
n pow x n pow

QED.

Notice why we needed the strong principle of mathematical induction in the above proof.
The lines
2
)) 2 / ( ( ) ( n pow n pow = and
2
)) 2 / ) 1 (( ( ) ( = n pow x n pow in the definition
relate pow(n) to pow of some smaller number, but not necessarily to pow(n 1).


Example. Any postage of 8 cents or more can be obtained using only 3-cent and 5-cent
stamps.

Proof. We will use the strong principle of mathematical induction.

Step 1. We need to show that P(8), P(9) and P(10) are all true. The reason we need to
check three base cases for step1 in this problem will be clearer after we have completed
step 2. P(8) is true since postage of 8 cents can be made from one 3-cent and one 5-cent
stamp. P(9) is true since postage of 9 cents can be made from three 3-cent stamps. Also,
P(10) is true since postage of 10 cents can be made from two 5-cent stamps. This
concludes step 1.
13

Step 2. Fix an integer 10 n and assume that P(8), P(9), ..., P(n) are all true. We want
to show that P(n + 1) is true. In other words, we want to know that we can create postage
in the amount of n + 1 cents using only 3-cent and 5-cent stamps. Since 11 1 + n we
know that 8 3 ) 1 ( + n . Thus, by assumption, we can create postage in the amount of
3 ) 1 ( + n using only 3-cent and 5-cent stamps. But then we can create postage in the
amount of (n + 1) cents by simply adding an additional 3-cent stamp. That proves P(n +
1) is true. QED

Notice in the above example we actually needed three base cases. Thats because in step
2 we ask if we can create postage in the amount of (n + 1) 3 cents. That 3 covers
three possible previous cases that need to be checked. If we were trying to show that
P(11) were true we would need to know that P(11 3) = P(8) is true. If we were trying
to show that P(12) were true we would need to know that P(12 3) = P(9) is true. If we
were trying to show P(13) were true we would need to know P(10) is true. Now, because
we do know that P(8), P(9), and P(10) are true, we can conclude that P(11), P(12), and
P(13) are true. Similarly, we can now conclude that P(14), P(15), and P(16) are true, and
so on.

Actually, when doing problems that use the strong principle of mathematical induction it
is not uncommon to work out the details of step 2 before completing step 1. Thats
because often the details of step 2 will tell you how many base cases you need to check in
step 1.


Exercises

1. Consider the following function:

+ =
=
. 2 for ) 1 ( 2 ) (
3 ) 1 (
n n f n f
f

Compute f(9) both top down and bottom up.


2. Consider the function

+ =
=
=
. 2 for ) 2 ( 2 ) 1 ( ) (
3 ) 1 (
1 ) 0 (
n n f n n f n f
f
f

Compute ) (n f for n = 0, 1, 2, 3, 4.


3. Exponentiation by Squaring. The following algorithm computes
n
x for any integer
0 n .
14

=
=
=
odd is if )) 2 / ) 1 (( ( ) (
even and zero an greater th is if )) 2 / ( ( ) (
1 ) 0 (
2
2
n n pow x n pow
n n pow n pow
pow

Let x = 3 and compute pow(2), pow(3), pow(4), and pow(18). How many multiplications
did you need to perform to compute pow(18)?

4. Consider the following recursive function

+
=
=

1 for
2
) 1 (
) (
1 ) 0 (
) 1 (
2
n
n f
n f
f
n f

a. Compute f(1), f(2), f(3), f(4), and f(5). Use at least 8 decimal place accuracy in all
your calculations.
b. Use your calculator to compute 2 and compare it to your answers in part a. What
do you suppose happens if we compute g(n) for large values of n in the following
formula?

+
=
=

1 for
2
) 1 (
) (
1 ) 0 (
) 1 (
3
n
n g
n g
g
n g

Supply some evidence to back up your assertion.


5. Find a recurrence relation for the number ) (n a of all bit strings of length n that dont
contain 3 consecutive ones. Then determine ) 6 ( a .


6. Compute the following sums and products.
a.

6
1
) 2 3 (
i
i b.

=
+ +
4
0
2
) 3 2 (
i
i i c.

=
3
3
2
k
k

d.

6
1
) 2 3 (
i
i e.

=
+
4
2
2
) 2 (
k
k
k



7. Compute the following sums.
a.

= =
|
|

\
|
+
3
1
4
1
2
) (
i j
j i b.

= =
|
|

\
|
5
1 1 i
i
j
j


8. Write a recursive function f(n) that gives the sum of the first n positive odd integers.


15
9. Let

=
=
n
i
i
i n f
1
) 1 ( ) ( .
a. Create a table showing the values of f(1), f(2), f(3), ... , f(10).
b. Write a recursive formula for f(n).


10. Write a recursive function to compute n!.

11. Use induction to prove that
2
3 3
3
1
1

=
+
=

n n
i
i
for every positive integer n.



12. Prove that

=
+
=
+
n
i
n i i
1
1
1
1
) 1 (
1
for all integers 1 n .


13. Use induction to prove that
2
) 1 (
1
+
=

=
n n
i
n
i
for all positive integers n.


14. Consider the function

=
=
1 for ) 1 ( 03 . 1 ) (
000 , 10 ) 0 (
n n f n f
f
.
Use induction to prove that
n
n f ) 03 . 1 ( 000 , 10 ) ( = for all integers 0 n .


15. Prove that

=
=
n
i
i n f
1
) ( can be computed using the recursive function

+ =
=
2 for ) 1 ( ) (
1 ) 1 (
n n n f n f
f

for all integers 1 n


16. Find the smallest positive integer
0
n such that
n
n 2 ! for every integer
0
n n .
Prove this using induction.


17. In logic, the hypothetical syllogism says that given three statements P, Q, and R, the
hypotheses Q P and R Q logically imply R P . (Symbolically this is written
16
) ( ) ( ) ( R P R Q Q P ). Use induction to show that, given n statements
n
A A A , , ,
2 1
K that ) ( ) ( ) ( ) (
1 1 3 2 2 1 n n n
A A A A A A A A

L for 2 n .


18. Assume you have a number of logical statements
n
A A A , , ,
2 1
K where 2 n , and
further assume that given any two statements
i
A and
j
A we have either
j i
A A or
i j
A A . Prove that there exists a statement
i
A such that
j i
A A for every i j .


19. Let

=
=
n
i
i
i n f
1
) 1 ( ) ( which you saw in problem 9. Prove that, for all positive
integers n,

+
=
odd is if 2 / ) 1 (
even is if 2 /
) (
n n
n n
n f .


20. Denote by
n
F the nth Fibonacci number (So 0
0
= F , 1
1
= F , and
2 for
2 1
+ =

n F F F
n n n
). Prove that

=
+
=
n
i
i n n
F F F
1
2
1
for 1 n .


21. Let 3
1
= u , 5
2
= u , and
2 1
2 3

=
n n n
u u u for 3 n . Show that 1 2 + =
n
n
u for all
1 n .

Вам также может понравиться