Comp Sci analysis Assignment3-Questions
Comp Sci analysis Assignment3-Questions
(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
Pn
(5 marks) 2. Consider the following algorithm that attempts to compute the value k · i=1 i for any real number
k and any integer n > 0. Prove that sumAlg(k, n) is fully correct.
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
3. Consider the following algorithm that attempts to compute the value y n for real number y ̸= 0 and
any integer n ≥ 0.
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.
(7 marks) 4. Prove that the loop in Algorithm 4 below eventually terminates. In particular: give a formula E
and prove that it satisfies the two conditions in the definition of loop measure. You should assume
(without proof) that the loop invariant is true each time the loop condition is checked.
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
Definitions
• The symbol Z represents the set of integers {. . . , −2, −1, 0, 1, 2, . . .}.
• The symbol N represents the set of positive integers {1, 2, 3, . . .}.
• The symbol R represents the set of real numbers.
• An integer n is even if there exists an integer k such that n = 2k.
• An integer n is odd if there exists an integer k such that n = 2k + 1.
• The floor of x, denoted by ⌊x⌋, is the largest integer that is less than or equal to x.
• The ceiling of x, denoted by ⌈x⌉, is the smallest integer that is greater than or equal to x.
• For every positive integer x, the double factorial x!! is defined as the product 1 · 3 · 5 · · · (x − 4) · (x − 2) · x when x is
odd, and, defined as the product 2 · 4 · 6 · · · (x − 4) · (x − 2) · x when x is even.
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/a⌉
• for any integers a, b: ⌊ b ⌋ = ⌊ ab
x
⌋ and ⌈ b ⌉ = ⌈ ab
x
⌉
Summation Identities
b n
X X n(n + 1)
1= b−a+1 j=
j=a j=1
2
n n
X n(n + 1)(2n + 1) X n2 (n + 1)2
j =
2
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
b
b b b b
X X X X 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