faiaz-halim

Untitled

Mar 3rd, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. Suppose a and b are constant integer arrays with domain {0, .., n} where n ≥ 1 with a increasing (∀i, j ∈ {0, .., n} · i ≤ j ⇒ a(i) ≤ a(j)) and b decreasing (∀i, j ∈ {0, .., n} · i ≤ j ⇒ b(i) ≥ b(j)). You may assume that a(0) ≤ b(0) and a(n) > b(n). We need to find where the arrays cross. In particular we want to find the largest value for p where a(p) ≤ b(p)
  2. f = ⟨a(p′) ≤ b(p′) ∧ a(p′ + 1) > b(p′ + 1)⟩
  3. Use the method of binary search.
  4. 1. Give a loop invariant I.
  5. 2. Let g = (⟨I⟩ ⇒ f) and m = ⟨a′ = a ∧ b′ = b ∧ I′⟩ Give an initialization command that refines m.
  6. 3. Give a guard expression A such that ⟨¬A⟩ ⇒ g ⊑ skip.
  7. 4. Give a loop body command that refines h = ⟨A ∧ I ⇒ I′ ∧ a′ = a ∧ b′ = b⟩ and decreases a bound expression.
  8. 5. Give a bound expression with which your loop can be shown to terminate.
  9.  
  10. (a) q, b, and a are constant functions. Let g = ⟨x′ = q(x, a(y))⟩ and f = ⟨x′ = q(b(x), a(y + 1))⟩. Give an assignment command h such that f ⊑ h; g
  11. (b) Use the forward substitution law for assignment to show that
  12. f ⊑ h; g.
  13.  
  14.  
  15. For integers X and Y design an efficient (log Y time) iterative algorithm to compute X^Y.
  16. Complete the following proof outline {x = X ∧ y = Y ≥ 0} ? {z = X^Y}
  17. You should assume that variables x, y, and z hold mathematical integers —thus there is no overflow– and that the operations assignment, +, -, ×, ÷, ‘odd’, and ‘even’ each take 1 unit of time, as do any comparisons. Be sure to state the invariant of your loop. Note that the solution to this problem is useful in the efficient
  18. implementation of the RSA algorithm, which is used widely for encryption.
  19.  
  20. Consider the following specification where a and b are arrays of integers of length n
  21. f = ⟨∀i ∈ {0, ..n} · b′(i) = (∑ j ∈ {0, .., i} · (−1)^j × a(j))⟩
  22. E.g. a = [7, 2, 6, 8], b′ = [7, 5, 11, 3].
  23. Read the whole question before beginning to answer. The idea is to
  24. develop a command of the form
  25. f ⊑ m0; while A do h0
  26. The command should run in Θ(n) time.
  27. (a) Propose a loop invariant I
  28. Let g = (⟨I⟩ ⇒ f). Let m = ⟨I′ ∧ a′ = a⟩ so that f is refined by m; g.
  29. (b) Give a command that refines m.
  30. (c) Give an expression A and such that
  31. I ∧ ¬A ⇒ ∀i ∈ {0, ..n} · b(i) = (∑ j ∈ {0, ..i} · (−1)^j × a(j))
  32. is true for all states, i.e., so that ⟨¬A⟩ ⇒ g is refined by skip.
  33. (d) Propose a bound expression for the loop.
  34. Let h = ⟨A ∧ I ⇒ I′ ∧ a′ = a⟩ so that
  35. g is refined by (if A then (h; g) else skip)
  36. (e) Give a command that refines h and that makes the bound
  37. expression smaller.
Advertisement
Add Comment
Please, Sign In to add comment