Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Below is the pseudocode for exponentiation by squaring Precondition: n >0 int power (int m, int n) ( int y - n; int result- 1;
Below is the pseudocode for exponentiation by squaring Precondition: n >0 int power (int m, int n) ( int y - n; int result- 1; while (y !- 0) if (y is even) else result result#x; - return result; Postcondition: result mn a) Find a suitable loop invariant. (3 pts) b) Show that the invariant holds before the loop (base case). (2 pts) c) Show by induction that if the invariant holds after k-th iteration, and execution takes a k+1-st iteration, the invariant still holds (inductive step). (6 pts) d) Show that the loop exit condition and the loop invariant imply the postcondition result - m*n. (2 pts) e) Find a suitable decrementing function. Show that the function decreases at each iteration and that when it reaches a minimum the loop is exited. (2 pts)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started