Assignment 3
1. Compute the following sums:
a. ∑
Answer:
=∑
=u–1+1
= (n + 1) – 3 + 1
=n+1–2
=n-2
b. ∑
Answer:
=∑
= [1 + 2] + … + n
=∑
+ (n + 1) – (1 + 2)
=∑
+ (n + 1) – 3
=∑
+n –2
= n(n + 1) + (n - 2)
2
= n2 + n + (n - 2)
2
= n2 + 3n – 4
2
c. ∑
Answer:
∑
=∑
=∑
= n (n+1) (2n + 1) + n (n+1)
6
2
= (n - 1) (n -1 + 1) (2 ( n –1) +1) + (n - 1) (n – 1 + 1)
6
2
= (n - 1) (n) (2n – 2 + 1) + (n – 1) (n)
6
2
2
= (n - n) (2n – 1) + (n – 1) (n)
6
2
= (n3 - n2 - 2n2 + n) + (n2 – n)
6
2
= 2n3 - 3n2 + n + n2 – n
6
2
= 2n3 - 3n2 + n + 3n3 – 3n
6
= 2n3 – 2n
6
= n3 – n
3
2
=n –1
3
2. Consider the following algorithm.
Algorithm Mystery( n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S←S+i*i
return S
a. What does this algorithm compute?
Answer:
2
S(n) = ∑
b. What is its basic operation?
Answer:
Multiplication
c. How many times is the basic operation executed?
Answer:
At least one time
d. What is the efficiency class of this algorithm?
Answer:
C(n) = n (n)
e. Suggest an improvement or a better algorithm altogether and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done.
Answer:
∑
2
= n (n + 1) (2n + 1)
6
3. Consider the following algorithm.
Algorithm Secret(A[0..n − 1])
//Input: An array A[0..n − 1] of n real numbers minval ← A[0]; maxval ← A[0] for i ← 1 to n − 1 do if A[i] < minval minval ← A[i] if A[i] > maxval maxval ← A[i] return maxval − minval
Answer questions a to e of Problem 2 about this algorithm.
Answer:
a) Comparison the range
b) Comparison element
c) Nothing other than 2 repeated n-1
d) (n)
e) if A[i] < minval minval A[i] else if A[i] > maxval maxval A[i]
4. Consider the following algorithm.
Algorithm Enigma(A[0..n − 1, 0..n − 1])
//Input: A matrix A[0..n − 1, 0..n − 1] of real numbers for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do if A[i, j] ≠