Consider the following program where a and n are positive in

Consider the following program, where a and n are positive integers. Input: a, n n y while (m 1) 1 if m is even m m/2 if m is odd m (m-1) /2; Output x*y

Solution

A loop variant is a condition which should be satisfied immediately before and after each iteration of a loop. The values of a and n should be given by user as per program given in the problem.

Assume the value of a and n entered by the user are 5 and 9 respectively. The loop invariant condition an = xm * y should be satisfied before and after each iteration of the while loop given in the program.

Initially the value of a and n are 5 and 9 respectively. The values of x, m, and y are initialized as follows:

x = a; m = n; y = 1;

Therefore, the value of x, m, and y are 5, 9, and 1 respectively. Check whether the condition an = xm * y is satisfied or not.

an = xm * y

59 = 59 * 1

59 = 59

Therefore, the condition is satisfied before the while loop. Execute the while loop for the given values of x, m, a, n, and y.

Iteration 1:

y = x*y

            y = 5*1

y = 5

            x = x*x

            x = 5*5

x = 25

            m = (m - 1)/2

            m = (9 - 1)/2

            m = 8/2

m = 4

Check whether the condition an = xm * y is satisfied after the first iteration. The value of a, m, x, and y are as follows:

a = 5, x = 25, m = 4, y = 25, n = 9

an = xm * y

59 = 254 * 5

59 = (52)4 * 5

59 = 58 * 5

59 = 58 + 1

59 = 59

The given loop invariant condition is also satisfied after the first iteration and before the next iteration of the while loop.

Iteration 2:

            x = x*x

            x = 25*25

x = 625

            m = m / 2

            m = 4 / 2

m = 2

Check whether the condition an = xm * y is satisfied after the first iteration. The value of a, m, x, and y are as follows:

a = 5, x = 625, m = 2, y = 5, n = 9

an = xm * y

59 = 6252 * 5

59 = (54)2 * 5

59 = 58 * 5

59 = 58 + 1

59 = 59

The given loop invariant condition is also satisfied after the first iteration and before the next iteration of the while loop.

Iteration 3:

            x = x*x

            x = 625*625

x = 390625

x = 54 * 54

            m = m / 2

            m = 2 / 2

m = 1

Check whether the condition an = xm * y is satisfied after the first iteration. The value of a, m, x, and y are as follows:

a = 5, x = 390625, m = 1, y = 5, n = 9

an = xm * y

59 = (54 * 54)1 * 5

59 = 54+4 * 5

59 = 58 * 5

59 = 58 + 1

59 = 59

The given loop invariant condition is also satisfied after the first iteration and before the next iteration of the while loop.

Iteration 4:

            output = x*y

            output = 390625*5

output = 1953125

Since the loop invariant condition an = xm * y is satisfied before and after each iteration of the while loop given in the program. Thus, it is proved by induction the while loop given in the program has the loop invariant: an = xm * y.

Hence, the given program computes a power of a raise to n i.e., an. since the value of a is 5 and n = 9. The program will compute an = 59 = 1953125. The output of the program is 1953125 if the value of a and n are 5 and 9 respectively.

 Consider the following program, where a and n are positive integers. Input: a, n n y while (m 1) 1 if m is even m m/2 if m is odd m (m-1) /2; Output x*y Soluti
 Consider the following program, where a and n are positive integers. Input: a, n n y while (m 1) 1 if m is even m m/2 if m is odd m (m-1) /2; Output x*y Soluti
 Consider the following program, where a and n are positive integers. Input: a, n n y while (m 1) 1 if m is even m m/2 if m is odd m (m-1) /2; Output x*y Soluti

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site