Question

Use a loop invariant to prove that the following program segment for computing the nth power,...

Use a loop invariant to prove that the following program segment for computing the nth power, where n is a positive integer, of a real number x is correct.

power := 1
i := 1
while i <= n
  power := power*x
  i := i+1

Homework Answers

Answer #1

Let p be the loop invariant and we need to prove that before, inside and termination of loop “power = x i−1 and i ≤ n + 1.”.

For iniatial case: i=1 and power = xi-1 = x0 =1;

In the loop: i<=n; Since I is incerement by 1; and we know that powerinitial =xi-1;

So, the new power will be powernew= powerinitial*x= xi-1*x = xi-1+1 = x(i+1)-1 ;

Now, We know that i=i+1; for new case

Then, on putting i in the place of (i+1), for new case

We get powernew= xi-1 ;

This process goes throughout the loop.

Now, after the termination of loop, ie. i<=n is false;

We get i=n+1;

by putting the value of i in power

powern = xi-1 = xn+1-1 = xn.

I hope it helps.
Thank you

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Recursively computing sums of cubes, cont. (a) Use induction to prove that your algorithm to compute...
Recursively computing sums of cubes, cont. (a) Use induction to prove that your algorithm to compute the sum of the cubes of the first n positive integers returns the correct value for every positive integer input.
Prove binary search (Seen below) is correct using a loop invariant. def search_binary(arr, val): low, up...
Prove binary search (Seen below) is correct using a loop invariant. def search_binary(arr, val): low, up = 0, len(arr) - 1 while low != up: # binary search mid = (low + up) / 2 if arr[mid] == val: return mid elif val < arr[mid]: up = mid - 1 else: low = mid + 1 return low
*********I need question 6 answered which is from question 5 which is ********* Question 5 :...
*********I need question 6 answered which is from question 5 which is ********* Question 5 : Program Correctness I (1 point) Use the loop invariant (I) to show that the code below correctly computes n P−1 k=0 2k (this sum represents the sum of the first n even integers where n ≥ 1). Algorithm 1 evenSum(int n) 1: p = 2(n − 1) 2: i = n − 1 3: while i > 0 do 4: //(I) p = nP−1...
Using a loop (not the output format character %o), create a program that takes a positive...
Using a loop (not the output format character %o), create a program that takes a positive integer n, and then displays the polynomial for the octal representation of that integer. Use successive division, as demonstrated in the binary conversion example from the lesson, to do this. For example, for n = 157, the program should output the following: 157 = + (5 * 8^0) + (3 * 8^1) + (2 * 8^2) When this part is working properly, “surround” this...
A program devise a recursive algorithm to find a2n, where a is a real number and...
A program devise a recursive algorithm to find a2n, where a is a real number and n is a positive integer (hint: use the equality a2n+1 = (a^2n)^2
Given the following program segment….             for (num = 10; num >= 1; num--)            cout...
Given the following program segment….             for (num = 10; num >= 1; num--)            cout << setw(3) << num; Write a while loop that will have the same output. C++
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
Prompt the user for a positive whole number (call it n) Use a loop to calculate...
Prompt the user for a positive whole number (call it n) Use a loop to calculate the following: Sum: 1+2+...+n Sum of squares: 1^2 + 2^2 + ... + n^2 factorial: 1 x 2 x ... x n Print out the numbers when the loop is done. Do this with a for loop and a while loop. Call the class LoopArithmetic Sample output: The sum from 1 up to 5 is: 15 The sum of squares 1 to 5 is:...
Prove by mathematical induction one of the following statements : a) 1 · 2 + 2...
Prove by mathematical induction one of the following statements : a) 1 · 2 + 2 · 3 + 3 · 4 + . . . + n(n + 1) = n(n+1)(n+2) 3 for all integer n ≥ 1. b) u1 − u2 + u3 − u4 + . . . + (−1)n+1un = 1 + (−1)n+1un−1 for all integer n ≥ 1. (un denotes the nth Fibonacci number)
Write a program that does the following and prints out the result          Use a do-while...
Write a program that does the following and prints out the result          Use a do-while loop and switch statement          Enter value x and a case number n          for n = 0 exit else continue          n=100 result = x * 9          n=102 result = x + 10 and continue          n=103 result = (x + 10) * x          n=104 and 106 result = x / x default    result = 99; test with x =...