Comp Sci Analysis Assignment3-Solutions
Comp Sci Analysis Assignment3-Solutions
SOLUTIONS
(3 marks) 1. For this question, I have provided a while loop and a loop invariant.
Let P(k) be the predicate: “LoopInv is true when the loop condition is checked for the k th time".
For an arbitrary k ≥ 2, I would like you to write a full proof of the statement P(k − 1) ⇒ P(k).
Algorithm 1
1: //LoopInv: a + b == 100
2: while a ≥ 0 do
3: a ← a+1
4: b ← b−1
5: end while
Solution:
(1) Notation: for any j ≥ 1, denote by a j and b j the values of variables a and b when the loop
condition on line 2 is checked for the j th time.
(2) Considering an arbitrary k ≥ 2, we prove P(k − 1) ⇒ P(k) using a Direct Proof: suppose that
P(k − 1) is true, i.e., LoopInv is true when the loop condition is checked for the (k − 1) th time,
and consider the k th time that the loop condition is checked.
(3) Since k ≥ 2 and the loop condition is being checked for a k th time, the loop condition must have
been checked for a (k − 1) th time.
(4) From (2), we know that LoopInv is true when the loop condition is checked for the (k − 1) th time,
so ak−1 + bk−1 = 100.
(5) During loop iteration k − 1, we see from the code that a increases by 1 and b decreases by 1, i.e.,
ak = ak−1 + 1 and bk = bk−1 − 1.
(6) So, when the loop condition is checked for the k th time, we see that
ak + bk = (ak−1 + 1) + (bk−1 − 1) from (5). Simplifying this gives ak + bk = ak−1 + bk−1 .
(7) From (4) and (6), we see that ak + bk = ak−1 + bk−1 and ak−1 + bk−1 = 100, which means that
ak + bk = 100. This proves that P(k) is true.
Algorithm 2 sumAlg(k, n)
1: //pre: (k ∈ R) ∧ (n ∈ Z) ∧ (n > 0)
2: if n == 1 then
3: answer ← k
4: else
5: answer ← (k · n) + sumAlg(k, n − 1)
6: end if
7: return answer Pn
8: //post: the return value is equal to k · i=1 i
Solution: Note: you may include k as a parameter of the predicate, but it is not necessary, since the value of
k isn’t modified in the recursive calls.
(2) We prove that the predicate P(n) is true for all k ∈ R and n ≥ 1, by induction on n. Consider an
arbitrary fixed k ∈ R.
(5) Consider n = 1. At line 2 of the code, the if condition evaluates to true, so line 3 is
executed next.
(6) At line 3, the value of answer is set to k, and line 7 is executed next.
(8) Induction Hypothesis: Assume, for some n ≥ 2, that P(1) ∧ · · · ∧ P(n − 1) is true.
(11) Since we are assuming that n ≥ 2, the if condition at line 2 evaluates to false, so line 5 is
executed next.
(13) It follows that, at line 5, after the call to sumAlg(k, n − 1) terminates, the variable answer
Pn−1
is assigned the value (k · n) + k · i=1 i.
Pn−1 Pn
(14) Factoring out k, we get that the value of answer is k · [n + i=1 i] = k · i=1 i
immediately after line 5.
(15) As the value ofP answer does not change after line 5, it follows that the value of answer
n
returned at line 7 is k · i=1 i (and the function terminates), which proves that P(n) is true.
Algorithm 3
1: //pre: ( y ∈ R) ∧ ( y ̸= 0) ∧ (n ∈ Z) ∧ (n ≥ 0)
2: answer ← 1
3: i ← 1
4: //LoopInv:
5: while i ≤ n do
6: answer ← answer ∗ y
7: i ← i+1
8: end while
9: //post: answer == y n
(5 marks) (a) Prove that the loop in Algorithm 3 eventually terminates. In particular: give a formula E and prove that
it satisfies the two conditions in the definition of loop measure.
Solution: Notation: for an arbitrary m ≥ 1 and any variable v, we write vm to denote the value of v
when the loop condition is checked for the m th time.
(2) Proof of Condition 1: between every two checks of the loop condition, the value of E
decreases by at least 1.
(3) Consider any k ≥ 2. By the definition of E, we know that Ek−1 = nk−1 − ik−1 + 1 and
Ek = nk − ik + 1.
(4) Consider the execution of the inner statements of the loop during the (k − 1) th loop
iteration. We see that n is not modified, so nk = nk−1 , and we see that i is incremented by 1,
so ik = ik−1 + 1.
(9) From (8), we know that i ≥ n + 1. Since the loop condition is i ≤ n, we have shown
that the loop condition evaluates to false.
(10) Since we proved that E is a loop measure, we have proven that the while loop eventually
terminates.
(1) Let P(k) be the predicate “LoopInv is true when the loop condition at line 5 is checked for
the k th time". We prove that P(k) is true for all k ≥ 1 by induction on k.
(2) Line 2 of the code sets answer to 1, and line 3 of the code sets i to 1. Therefore, the first time
that the loop condition is checked, we see that i = 1 ∈ Z and that answer = 1 = y 0 = y i−1 .
(3) From pre, we know that n ≥ 0 at line 1, and we see that the value of n is not modified in
lines 2-3, so n ≥ 0 the first time the loop condition is checked. Therefore, n + 1 ≥ 1 the first
time the loop condition is checked. From (2), we know that i = 1 immediately before line 5,
so we conclude that n + 1 ≥ i the first time the loop condition is checked.
(4) From (2) and (3), we see that (answer == y i−1 ) ∧ (i ≤ n + 1) ∧ (i ∈ Z) the first time that the
loop condition at line 5 is checked, which proves that P(1) is true.
Inductive Step:
(5) By the Induction Hypothesis, P(k − 1) is true, so LoopInv is true when the loop condition
was checked for the (k − 1) th time. From LoopInv, this means that
(answerk−1 == y ik−1 −1 ) ∧ (ik−1 ≤ n + 1) ∧ (ik−1 ∈ Z).
(6) Consider the execution of the inner statements of the loop during the (k − 1) th loop iteration.
We see that answer is multiplied by y, and then i is incremented by 1. In other words,
answerk = answerk−1 ∗ y and ik = ik−1 + 1.
(7) From (5) and (6), we start with answerk = answerk−1 ∗ y and substitute in
answerk−1 == y ik−1 −1 to get answerk = y ik−1 . Since ik = ik−1 + 1, this proves that
answerk = y ik −1 . Also, since ik = ik−1 + 1 and ik−1 ∈ Z, we conclude that ik ∈ Z.
(8) The fact that we are checking the loop condition for a k th time means that the (k − 1) th check
of the loop condition evaluated to true. From the loop condition on line 5, it follows that
ik−1 ≤ n.
(9) From (6) and (8), since ik = ik−1 + 1 and ik−1 ≤ n, we conclude that ik ≤ n + 1.
(10) Putting together our conclusions from (7) and (9), we have shown that
(answer == y i−1 ) ∧ (i ≤ n + 1) ∧ (i ∈ Z) when the loop condition is checked for the k th time.
This proves P(k) is true, and the induction is complete.
Solution:
(1) Suppose that LoopInv is true immediately before each check of the loop condition. Suppose
that line 9 is reached, which means that the loop condition has evaluated to false when line 9
is reached.
(3) LoopInv tells us that i ≤ n + 1 and i ∈ Z. From (2), we know that i > n, which, together
with the fact that i ∈ Z, means that i ≥ n + 1. So we have (i ≥ n + 1) ∧ (i ≤ n + 1), which
implies that i = n + 1.
(4) From LoopInv, we know that answer = y i−1 at the final loop check, and so answer = y i−1
when line 9 is reached as well.
(5) From (3) and (4), we have that (answer = y i−1 ) ∧ (i = n + 1) at line 9, which implies that
answer = y n at line 9. This means post is true when line 9 is reached.
Algorithm 4
1: //LoopInv: (x, y ∈ Z) ∧ ( y ≥ 0)
2: while (x ≥ 3) ∧ ( y ≤ 100) do
3: if x is odd then
4: y ← y +1
5: x ← x +1
6: else
7: x ← x −3
8: end if
9: end while
Solution: Note: there are lots of correct possibilities here for E. For example, anything of the form x −α y +β
where α ≥ 2 and β ≥ 198 should work, but I’m sure there are many more.
Notation: for an arbitrary m ≥ 1 and any variable v, we write vm to denote the value of v when the loop
condition is checked for the m th time.
(2) Proof of Condition 1: between every two checks of the loop condition, the value of E decreases by
at least 1.
(3) Consider any k ≥ 2. By the definition of E, we know that Ek−1 = x k−1 − 2 yk−1 + 200 and
Ek = x k − 2 yk + 200.
(4) Consider the execution of the inner statements of the loop during the (k − 1) th loop
iteration. There are two sub-cases to consider:
• Case (a): the if condition evaluates to true (i.e., x is odd). We see that both x and y are
incremented, so x k = x k−1 + 1 and yk = yk−1 + 1.
Substituting these values into our equation for Ek , we get
Ek = (x k−1 + 1) − 2( yk−1 + 1) + 200 = x k−1 + 1 − 2 yk−1 − 2 + 200 = Ek−1 − 1.
In other words, E decreases by 1.
• Case (b): the if condition evaluates to false (i.e., x is even). We see that x decreases by 3,
and y is not changed, so x k = x k−1 − 3 and yk = yk−1 .
Substituting these values into our equation for Ek , we get
Ek = (x k−1 − 3) − 2( yk−1 ) + 200 = x k−1 − 3 − 2 yk−1 + 200 = Ek−1 − 3.
In other words, E decreases by 3.
(5) Proof of Condition 2: if E ≤ 0, then the loop condition evaluates to false.
Floors/Ceilings
For any real number x and any real number y ̸= 0:
• −⌊x⌋ = ⌈−x⌉ and −⌈x⌉ = ⌊−x⌋
• for any integer k: ⌊k⌋ = ⌈k⌉ = k
• for any integer k: k + ⌊x/ y⌋ = ⌊k + x/ y⌋
• for any integer k: k + ⌈x/ y⌉ = ⌈k + x/ y⌉
• for any integer k: ⌊(k + 1)/2⌋ = ⌈k/2⌉ and ⌈(k − 1)/2⌉ = ⌊k/2⌋
⌊x/a⌋ x ⌈x/a⌉ x
• for any integers a, b: ⌊ b ⌋ = ⌊ ab ⌋ and ⌈ b ⌉ = ⌈ ab ⌉
Summation Identities
b n
X X n(n + 1)
1= b−a+1 j=
j=a j=1
2
n n
X
2 n(n + 1)(2n + 1) X n2 (n + 1)2
j = j3 =
j=1
6 j=1
4
n ∞
X q n+1 − 1 X 1
∀q > 0, q ̸= 1, qj = ∀q, 0 < q < 1, qj =
j=0
q−1 j=0
1−q
Xb Xb Xb b
X b
X
( f ( j) + g( j)) = f ( j) + g( j) c · f ( j) = c · f ( j) for any constant c
j=a j=a j=a j=a j=a