Name:__________________________________________________ UFID:____________________________
COT 3100 Spring 2010
Midterm 2
For the first two questions #1 and #2, do ONLY ONE of them. If you do both, only question #1 will be graded.
1. (20 pts) Give iterative and recursive algorithms for finding the nth term of the sequence {an} defined by
a0 = 2, a1 = 2 and an+1 = an×(an-1)2. Which algorithm is more efficient and why?
Solution:
+ Input: n, a0 = 2, a1 = 2, an+1 = an×(an-1)2;
+ Output: The nth term of the sequence {an}.
Iterative algorithm:
for (int i = 2; i <=n; i++) {
result = a1 * a0 * a0;
a0 = a1;
a1 = result;
}
return result;
Recursive algorithm:
int a(int k) {
if (k <= 1 )
return 2;
else
return a(k-1)*a(k-2)*a(k-2);
}
return result = a(n);
Which algorithm is more efficient and why?
Iterative algorithm performs (n-2) iterations and in each iteration, updates values of 3 variables, thus its time
complexity is O(n). The recursive algorithm, in order to find a(n), loops into itself 3 times to find a(n-1) and
two a(n-2). To find a(n-1), it again loops into itself 3 times to find a(n-2) and two a(n-2), so on and so forth.
Thus, its creates a complete 3-leaf tree with height n. Thus, the total time complexity is O(3n). Therefore,
the iterative algorithm is more efficient.
1
Name:__________________________________________________ UFID:____________________________
2. (20 pts) Give iterative and recursive algorithms for determining whether a string is a palindrome or not.
(Note: A string is a palindrome if it can be read the same way in either direction, e.g., ‘ABBA’, ‘madam’,
‘level’).
Solution
+ Input: A string w of length n;
+ Output: YES if w is a palindrome, NO otherwise.
Iterative algorithm
w is a palindrome iff (w[i] == w[n+1-i]) for any ith index from 1 to n. Therefore, we test this property on
every ith position and stop when the index i hits the middle of the string w.
int middle = n/2;
for(int i = 1; i<=middle; i++) {
if ( w[i] != w[n+1-i] )
return NO;
}
return YES;
Recursive algorithm
A string w of length k is a palindrome iff (w[1] == w[k]) and the substring w[2...k-1] is also a palindrome.
This observation suggests the following recursive algorithm
boolean isPal(w, k) {
if (k==0 || k==1)
return YES;
return (w[1] == w[k]) && isPal(w[2...k-1], k-2);
}
2
Name:__________________________________________________ UFID:____________________________
3.
(10 pts) Prove by induction that (x - y) divides xn - yn for any integer n ≥ 1.
Solution
+ Let P(n) be the statement: (x - y) divides xn - yn.
+ Basis step:
n = 1. It is clear that (x - y) divides x1 - y1 (= x - y).
+ Inductive step:
- Assume P(n) is true. We will prove that P(n+1) is also true, i.e., (x-y) divides xn+1 - yn+1.
- Now:
xn+1 - yn+1 = xn+1 - xyn + xyn - yn+1 = x(xn - yn) + yn(x - y)
- Since P(n) is true, we have (x - y) divides xn - yn, which implies (x-y) divides x(xn - yn). In addition,
(x - y) also divides yn(x - y). Therefore, (x - y) divides x(xn - yn) + yn(x - y), which means P(n+1) is also
true.
4. (20 pts) Let S be the subset of the set of ordered pairs of integers defined recursively by:
Basis step: (0,0) ∈ S.
Recursive step: If (a,b) ∈ S then (a+4, b+5) ∈ S and (a-2, b-7) ∈ S.
a. Prove by strong induction on the number of applications of recursive steps that a×b is even for any
(a,b) ∈ S.
Solution
Let n be the number of applications of recursive steps. We will show that for any (a,b) ∈ S, a is even
which implies a×b is even for any (a,b) ∈ S
Basis step
n = 0. It is clear that 0 is even. Thus the basis step is true.
Inductive step
+ Assume that for all 0 < n ≤ N, a is even for any (a,b) ∈ S. We will show that a' is also an even
number for any (a',b') generated in step (N+1).
+ Consider the two following cases. Case #1: a' is derived by (a+4) for some number a in step N and
based on the assumption, we have a is even. Thus a' = a + 4 is also even. Case #2: a' is derived by
(a-2) for some number a in step N and based on the assumption, we have a is even. Thus a' = a - 2 is
also even. The inductive step is complete.
a. Prove by structural induction that 9 | a+ b for any (a,b) ∈ S
Solution
Basis step
It is clear that 9 | 0 + 0. The basis step is true.
Inductive step
+ We will prove that, in Recursive Step, any (a', b') obtained from (a,b) ∈ S satisfies: 9 | (a' + b'). Since
(a', b') is derived from (a,b), it must be obtained by either (a+4,b+5) or (a-2,b-7). Thus, (a' + b') equals either
(a+b+9) or (a+b-9)
(*).
+ Now, because (a,b) is previously in S, we have 9 | (a + b) which implies 9 divides both (a+b+9) and
(a+b-9). This fact also implies that 9 | (a' + b') due to (*).
3
Name:__________________________________________________ UFID:____________________________
5. (10 pts) Give the recursive definition of the sequence {an}, n = 1,2,3,… if
a. an = 4n - 9
a1 = -5;
an+1 = 4(n+1) - 9 = 4n - 9 + 4 = an + 4;
b. an = n2 – 2n
a1 = -1;
an+1 = (n+1)2 - 2(n+1)
= n2 + 2n + 1 - 2n - 2
= n2 - 2n + 2n - 1
= an + 2n - 1
For questions #6 and #7, please justify your answers. Answers with only numbers will result in no point.
6. (10 pts) How many positive integers from 1000 to 9999 inclusive are divisible by 9 but not by 4?
Solution
"A number divisible by 9 but not by 4" means a number divisible by 9 but not 36(=4*9). Therefore, we
just need to find the number of integers divisible by 9 and subtract the number of those divisible by
36.
+ Number of integers divisible by 9
+ Number of integers divisible by 36
+ Thus, the answer is
1000 - 250 = 750.
7. a. (10 pts) A coin is flipped 20 times where each flip comes up either heads or tails. How many possible
outcomes contain at most 5 tails?
Solution
+ Number of outcomes contain 0 tails: C(20,0)
+ Number of outcomes contain 1 tails: C(20,1)
+ Number of outcomes contain 2 tails: C(20,2)
+ Number of outcomes contain 3 tails: C(20,3)
+ Number of outcomes contain 4 tails: C(20,4)
+ Number of outcomes contain 5 tails: C(20,5)
Total possible outcomes = C(20,0) + C(20,1) + C(20,2) + C(20,3) + C(20,4) + C(20,5)
4
Name:__________________________________________________ UFID:____________________________
b. (10 pts) What is the least number of people needed to form a group in which we can surely conclude that
there are at least 3 people having the same birthday?
Solution
Assume that there are 365 days in a year. Based on Pigeonhole Principle, the least number of people we
need to choose is one more than the number of days in (3-1) years. Answer = 365*(3-1) + 1
8. (10 pts) Find the error in this "proof" of the clearly false claim: "Every set of lines in the plane, no two of
which are parallel, meet in a common point".
"Proof": Let P(n) be the statement that "Every set of n lines in the plane, no two of which are parallel, meet in
a common point". We will try to prove that P(n) is true for n ≥ 2.
1. Basis step: P(2) is true due to the definition of parallel lines.
2. Inductive step: Assume that P(k) is true, i.e., every set of k lines in the plane, no two of which are parallel,
meet in a common point. We will try to prove that P(k+1) is also true.
3. Consider the set S of (k+1) distinct lines in the plane, no two of which are parallel. Since P(k) is true, the
first k lines of S must meet in a point p1, and the last k lines of S must meet in a point p2.
4. If p1 is different from p2, then all lines containing both of them must be exactly the same since two distinct
points determine a line. This implies the second line, the third line,..., and the kth lines of S are the same,
contradicting to the fact that they are all distinct.
5. Therefore, p1 and p2 must be the same point, i.e., (k+1) distinct lines in the plane, no two of which are
parallel, meet in a common point, which means P(k+1) is true. □
Solution
The problem is at step #4. We cannot show that P(2) implies P(3). When k = 2, the first two lines must
meet in a common point p1 and the last two lines must meet in a common point p2. But in this case, p1
and p2 do not have to be the same, because only the second line is common to both sets of lines.
5
Name:__________________________________________________ UFID:____________________________
Bonus questions:
1. (5pts) Show that
is Θ(
Solution
=
Choose c1 =
and c2 =
= Θ(
, we have
≤ c2×
c1 ×
Thus,
) for any positive integer k > 1.
.
)
2. (5 pts) Prove that if five points are selected from the interior of a 1×1 square, then there are two points
whose distance is less than
.
Solution
We first divide the square into 4 smaller squares (by connecting the corresponding middle points) and then
select 5 points from the interior of the "big" square. By Pigeonhole Principle, there must be a "small" square
containing at least 2 points. In addition, the maximum distance in this "small" square cannot exceed the
length of the diagonal, which is
. Thus, any two points inside this "small" square has distance less than
.
6
© Copyright 2026 Paperzz