0

Given the pseudocode

MUL(a,b) 
   x=a
   y=0
   WHILE x>=b DO
      x=x-b
      y=y+1
   IF x=0 THEN
      RETURN(true)
   ELSE
      RETURN(false)

Let x(n) and y(n) denote the value of x and y after the while loop has run n times.

I have to show by the proof of induction that

x(n) + b*y(n) = a

What I've done:

P(n): x(n) + by(n) = a

Let a and b be arbitrary numbers then the first loop will give x(1) = a - b and y(1) = 0 + 1 = 1

P(1): x(1) + by(1) = a <=> a = a

so P(1) is true.

Assume P(n) is true. We want to show that P(n+1) is also true.

For step n + 1 the loop will give x(n+1) = x(n) - b and y(n+1) = y(n) + 1

P(n+1): x(n+1) + by(n+1) = a <=> x(n) + by(n) = a

Using the assumption that P(n) is true, it follows that P(n+1) is also true, and the proof is complete.

My question: Since this is my first time using the proof of induction on a pseudocode, I'm not sure how to go about it. I just want to know if this is the right way to work around the problem, and if not what should the process be like?

Thanks

Labbiqa
  • 157
  • 8
  • Looks fine to me, although it seems easier to start with P(0) instead of P(1). And in fact, you should start with P(0), otherwise you have not covered the case of 0 iterations (when a < b). – trincot Oct 18 '16 at 20:43
  • @trincot but x(n) and y(n) are only defined for the while-loop and the while loop only runs for a >= b so shouldn't I start with P(1)? – Labbiqa Oct 18 '16 at 20:56
  • When the condition of the while loop is first executed, which x is being evaluated? Isn't it x(0)? Both x and y are initialised before the loop starts, so they are at that moment x(0) and y(0). – trincot Oct 18 '16 at 20:59
  • I see. That would give x(0) = a and y(0) = 0, so P(0): x(0) + by(0) <=> a + b*0 = a <=> a = a – Labbiqa Oct 18 '16 at 22:27
  • Possible duplicate of [Prove how the algorithm works](http://stackoverflow.com/questions/40119116/prove-how-the-algorithm-works) – Ami Tavory Oct 18 '16 at 22:32
  • @AmiTavory No, it's different questions – Labbiqa Oct 18 '16 at 22:40

1 Answers1

0

As you got it (almost) right in your question, answering this feels like overkill, but I'll do it anyway:

Your approach is fine, although you should not ignore the case where no loop iterations are executed at all, which is the case when a < b and corresponds to P0. If you prove P1 is true, and Pn+1 is true when Pn is true, you don't say anything about P0.

So with that slight modification, the derivation of the proof by induction goes as follows:

Define Pn as the value of the expression x + b*y after n iterations of the loop have been executed:

Pn : xn + b.yn = a

It is to be proved that Pn is true for all n >= 0

1. Base Case: P0

Note that this is a possible case: when the condition of the while loop is first evaluated, no iterations have been performed yet, so n is 0, yet we do have the values for the two variables at play: x0 and y0.

The pre-condition for the loop is determined by the assignment statements that precede the loop:

x=a
y=0

So we have:

P0 : x0 + b.y0 = a + b.0 = a

2. Inductive Step: Pn+1

Here we assume that Pn is true for a given n:

Pn : xn + b.yn = a

This is the pre-condition when starting the next iteration of the loop, in which the following statements are executed:

x=x-b
y=y+1

By substitution we get the post-condition for that particular iteration which by definition is Pn+1:

Pn+1 : (xn - b) + b.(yn + 1) = xn + b.yn = Pn = a

trincot
  • 317,000
  • 35
  • 244
  • 286