Sei sulla pagina 1di 10

CSCI 2041: Homework #5

Due on October 20, 2014

Professor Van Wyk Lab 012

Brian Zhang (ID: 5131091)

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Problem 1
We would like to show that
n N, P (n) : power n x = xn
P (0), the base case: We would like to show that power 0 x = x0 .

power 0 x = 1.0, by definition of power


1.0 = x0 , by properties of exponents
power 0 x = x0
P (n), the inductive case: We would like to show that power n x = xn .
Inductive hypothesis: power (n 1) x = xn1 holds.

power n x = x . power (n 1) x, by definition of power


= x . xn1 , by inductive hypothesis
= xn , by properties of exponents
power (n 1) x = xn1 = power n x = xn
n N, power n x = xn

Problem 2
Principle of induction:
n nat, P (n) if P (Zero) and P (Succ n) when P (n)
We would like to show that
n nat, P (n) : power n x = xtoInt n
P (Zero), the base case: We would like to show that power Zero x = xtoInt Zero .

power Zero x = 1.0, by definition of power


= x0 , by properties of exponents
= xtoInt Zero , by the definition of toInt
power Zero x = xtoInt Zero
P (Succ n), the inductive case: We would like to show that power (Succ n) x = xSucc n .
Inductive hypothesis: power n x = xn holds.

power (Succ n) x = x . power n x, by definition of power


= x . xtoInt n , by inductive hypothesis
= x1+(toInt n) , by properties of exponents
= xtoInt (Succ n) , by definition of toInt
power n x = xn = power (Succ n) x = xSucc n
n nat, power n x = xtoInt n
2

Homework #5

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Homework #5

Problem 3
Principle of induction:
l list, P (l) if P ([]) and P (v :: l0 ) when P (l0 )
We would like to show that
l, r list, P (l, r) : length (l @ r) = length l + length r
Let r be arbitrary in list. We consider the cases for l list.
P ([], r), the base case: We would like to show that length ([] @ r) = length [] + length r.

length ([] @ r) = = length r, by properties of list


= 0 + length r, by additive identity
= length [] + length r, by the definition of length
length ([] @ r) = length [] + length r
P (x :: xs, r), the inductive case:
We would like to show that length (x :: xs @ r) = length x :: xs + length r.
Inductive hypothesis: length (xs @ r) = length xs + length r holds.

length (x :: xs @ r) = length (([x] @ xs) @ r), by properties of list


= length ([x] @ (xs @ r)), by associativity of @ in list
= length (x :: (xs @ r)), by properties of list
= 1 + length (xs @ r), by definition of length
= 1 + length xs + length r, by inductive hypothesis
= (1 + length xs) + length r, by associativity of addition
= length x :: xs + length r, by definition of length
length (xs @ r) = length xs + length r = length (x :: xs @ r) = length x :: xs + length r
l, r list, length (l @ r) = length l + length r

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Homework #5

Problem 4
We would like to show that
l list, P (l) : length (reverse l) = length l
P ([]), the base case: We would like to show that length (reverse []) = length [].
This follows from the definition of reverse.
P (x :: xs), the inductive case:
We would like to show that length (reverse x :: xs) = length x :: xs.
Inductive hypothesis: length (reverse xs) = length xs holds.

length (reverse x :: xs) = length ((reverse xs) @ [x]), by definition of reverse


= length (reverse xs) + length [x], demonstrated in Problem 3
= length xs + length x, by inductive hypothesis
= length xs + (1 + length []), by definition of length
= length xs + 1, by definition of length
= 1 + length xs, by commutativity of addition
= length x :: xs, by definition of length
length (reverse xs) = length xs = length (reverse x :: xs) = length x :: xs
l list, length (reverse l) = length l

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Homework #5

Problem 5
We would like to show that
l1, l2 list, P (l1, l2) : reverse (l1 @ l2) = reverse l2 @ reverse l1
Let l2 be arbitrary in list. We consider the cases for l1 list.
P ([], l2), the base case: We would like to show that reverse ([] @ l2) = reverse l2 @ reverse [].

reverse ([] @ l2) = = reverse l2, by properties of list


= reverse l2 @ [], by properties of list
= reverse l2 @ reverse [], by the definition of reverse
reverse ([] @ l2) = reverse l2 @ reverse []
P (x :: xs, l2), the inductive case:
We would like to show that reverse (x :: xs @ l2) = reverse l2 @ reverse x :: xs.
Inductive hypothesis: reverse (xs @ l2) = reverse l2 @ reverse xs holds.

reverse (x :: xs @ l2) = reverse (([x] @ xs) @ l2), by properties of list


= reverse ([x] @ (xs @ l2)), by associativity of @ in list
= reverse (x :: (xs @ l2)), by properties of list
= reverse (xs @ l2) @ [x], by definition of reverse
= (reverse l2 @ reverse xs) @ [x], by inductive hypothesis
= reverse l2 @ (reverse xs @ [x]), by properties of list
= reverse l2 @ reverse x :: xs, by definition of reverse
reverse l2 @ reverse xs = reverse (x :: xs @ l2) = reverse l2 @ reverse x :: xs
l1, l2 list, reverse (l1 @ l2) = reverse l2 + reverse l1

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Problem 6
We would like to show that
e elements, l list, P (l, e) : sorted l sorted (place e l)
P ([], e), the base case: We would like to show that sorted [] sorted (place e []).

sorted [] = true, by the definition of sorted


sorted (place e []) = sorted [e] , by the definition of place
= true, by the definition of sorted
Since both sides of the implication are always true, the implication is true.
P (x :: xs, e), the inductive case:
We would like to show that sorted x :: xs sorted (place e x :: xs).
Inductive hypothesis: sorted xs sorted (place e xs) holds.
Let xi be the ith element of x :: xs. We break down the inductive case into three sub-cases:
Case: xs = [], so x :: xs = x :: [] = [x]
sorted [x] = true, by definition of sorted
If e < x, then sorted (place e [x]) = sorted e :: x , by definition of sorted
sorted e :: x = (e x) && sorted [x] , by definition of sorted
= (e x) && true, by definition of sorted
= true by case condition
Else e x, then sorted (place e [x]) = sorted x :: (place e []) , by definition of sorted
= sorted x :: e , by definition of sorted
= (x e) && sorted [e] , by definition of sorted
= (x e) && true, by definition of sorted
= true by case condition
Since both sides of the implication are always true, the implication for this case holds.
continued on next page...

Homework #5

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Homework #5

Case: e < x, xs 6= []
Assume sorted x :: xs holds. We will show that sorted (place e x :: xs) follows given the inductive
hypothesis.

sorted (place e x :: xs) = sorted e :: x :: xs, by definition of place


= (e x) && sorted x :: xs, by definition of sorted
= (e x) &&; true, by the assumption
= true, by the case condition

Case: e x, xs 6= []
Assume sorted x :: xs holds. We will show that sorted (place e x :: xs) follows given the inductive
hypothesis.
sorted (place e x :: xs) = sorted (x :: (place e xs)), by definition of place
Let x0 be the first element of xs. By assumption, sorted x :: xs = true, so (x x0 ) is true and sorted xs
is true by the definition of sorted.
sorted x :: xs = true sorted xs = true sorted (place e xs) = true

sorted (place e x :: xs) = sorted (x :: (place e xs)), shown above


= (x x0 ) && sorted (place e xs), by definition of sorted
= true && true, shown above
= true

The base case and all subcases of the inductive case have been shown to be true, therefore we can conclude:
e elements, l lists, sorted l sorted (place e l)

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Homework #5

Problem 7
iselem e (place e l) evaluates to true even for unsorted lists because place inserts e into l such that:
1. All elements at lower indices in the list are less than e.
2. e is less than the value at the next index.
iselem searches for e in l by going from the lowest index to the highest. It gives up once it finds an element
in the list greater than e.
Therefore, if e is placed in the l, iselem will not encounter a value in l larger than e before encountering
e. Sorting is not necessary.
The premise sorted l is necessary for question 6. If the entire list is unsorted, there is no way place can
guarantee a sorted list by merely inserting one element.

Problem 8
Loop invariant: z = y i
1. The precondition is that x, y, z, i are integers and x 0. Before the loop,
z = 1, i = 0
yi = y0 = 1 = z
z = y i before entering the loop
2. We have showed that before the loop, z = y i . Only z and i get updated each iteration, so let z 0 and i0
be z and i after one iteration.
0

z 0 = z y = y i y = y i+1 = y i

invariant still holds after 1 iteration


3. When the loop terminates, the negation of the loop condition and the loop invariant, which we have
shown to hold, implies the post condition.
The loop condition is i < x. The negation of this is i x. We can rule out i > x because initially,
x 0 and i = 0, so x i. i is only incremented by one each iteration so it will not become greater
than x.
loop condition i = x
z = y i by the loop invariant
(z = y i ) (i = x) (z = y x ), the post condition

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

Problem 9
tab tab tab

( * Loop invariant: lower n upper * )


( * Loop precondition: n 1.0, lower and upper are floats * )
float accuracy = .01;
float guess;
float lower = 1.0;
float upper = n;
while ( upper - lower > accuracy ):
guess = (upper + lower) /(2.0);
if (guess guess) > n then upper = guess;
else lower = guess
return (lower, upper);
1. Before the loop,
lower = 1.0
Given n 1.0, so n lower

n lower = lower

upper = n
n 1.0 n2 n
Given n 1.0, so upper = n

lower

n upper before entering the loop

Homework #5

Brian Zhang (ID: 5131091)

CSCI 2041 (Professor Van Wyk, Lab 012)

2. We have showed that before the loop, lower

Homework #5

n upper.

Let lower0 and upper0 be lower and upper updated after one iteration.
There are two cases:
Case 1: ((lower + upper)/2.0)2 > n
((lower + upper)/2.0)2 > n 1.0 (lower + upper)/2.0 >

upper0 = (lower + upper)/2.0 > n

lower0 = lower n

lower0 n upper0 , invariant holds

Case 2: ((lower + upper)/2.0)2 n


((lower + upper)/2.0)2 < n (lower + upper)/2.0 <

upper0 = upper n

lower0 n upper0 , invariant holds

3. When the loop terminates, the negation of the loop condition and the loop invariant, which we have
shown to hold, implies the post condition.
loop condition upper lower accuracy = .01

lower

n upper by the loop invariant

The conjunction of these is the post-condition.

10

Potrebbero piacerti anche