foo (start an integer, end an integer which is larger

foo (start an integer, end an integer which is larger than start)
total ← 0
i ← start
While (i <= end) add i to total
increment i
Return total
1. What do you think this code does?
2. Write down a loop invariant you could use to show that this algorithm correctly does what you claim.
After t times through the while loop, …
3. Suppose your loop invariant is true. How can you show the algorithm is correct based on this?
4. Now, prove your loop invariant by induction. Let t be the number of times through the while loop.
a. Base Case: Show the loop invariant is true before the while loop ever executes, when t=0.
b. Inductive Hypothesis: Suppose the loop invariant is true after t times through the while loop.
c. Induction Step: Now show that the loop invariant is true after t+1 times through the while loop.
A robot stands at the origin of a coordinate plane. The robot has been equipped with four types of
movement:
Northeast move in the direction (1,1)
Northwest move in the direction (-1,1)
Southeast move in the direction (1,-1)
Southwest move in the direction (-1,-1)
The robot starts at the origin and wants to reach the point (1,0) using any sequence of moves it knows
how to do. Will the robot be able to reach its destination?
Guidance and hints for robot question:
1) To answer “yes,” you must find a sequence of moves that gets the robot to (1,0). To answer “no,”
you must prove that it is impossible for the robot to reach (1,0).
2) Mark all the points where you know the robot can reach. Is (1,0) on this list?
3) What do all the points that you've marked have in common? This is the heart of the proof.
4) Big hint: Any x value is possible. Any y value is possible. But only certain (x,y) combinations are
possible.
5) State an invariant about the robot's position.
After any number of moves, ….
6) Prove the invariant by induction on the number of moves the robot has made.
7) Now answer the question based on the invariant.
Prove these two facts about convex polygons using induction.
1) The sum of the interior angles of any convex n-gon is (n-2)*180 degrees.
a. Base Case:
b. Inductive Hypothesis: Suppose …
c. Inductive Step:
2) Any convex n-gon has n(n-3)/2 diagonals.
a. Base Case:
b. Inductive Hypothesis: Suppose …
c. Inductive Step:
Guidance and hints for polygon questions:
1) Base case: What is the smallest value of n that makes sense when talking about a convex n-gon?
2) Inductive step: How can a polygon be built up from a smaller polygon?
3) Big hint: What happens when you remove a vertex from a polygon? How can you use the inductive
hypothesis to help you here?
Suppose you are designing a new Tetris piece. A Tetris piece is a region made up of square cells of the
same size, aligned so that they could fit in a grid. Here are some example Tetris pieces:
You would like to design your new Tetris piece to have perimeter 15. You can use any number of cells.
How should you design your new piece?
Guidance and hints for Tetris problem:
1) First, see if you can try to build a Tetris piece with perimeter 15.
2) If you're having trouble, keep track of the different perimeters you can build.
3) What do these numbers have in common? What does this say about your ability to build a Tetris
piece with perimeter 15?
4) How can you state your observation so that it can be proved by induction? Remember, induction
proofs are used to show that for all n greater than or equal to some value, a property is true. What
variable are you inducting on?
5) Prove your observation by induction on the number of cells in the Tetris piece.
6) Conclude that it is impossible to build a Tetris piece with perimeter 15.