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