-5

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)

I have to prove how the algorithm works.

So far I have only explained how it works, but I am not sure how you are supposed to prove how it works.

Edit: Just to clarify. I did ask this question on another thread. But the two questions are separate. The assignment I am working on consists of 3 questions. The 1st question is where I explained how the algorithm works. The 2nd question is on my other thread and the 3rd question (this thread) is where I have to prove how the algorithm I explained in question 1 works. The reason why I asked this question is not to get the answer to the question, but simply an explationation as to what I am asked to do since I can't seem to figure it out myself. Personally I would just do the same as the 1st question, but that wouldn't make much sense.

Community
  • 1
  • 1
Labbiqa
  • 157
  • 8
  • What would that *explanation* be? You haven't even said *what* it is supposed to accomplish, much less *how*. – Scott Hunter Oct 18 '16 at 22:29
  • You might also look at [this question](http://stackoverflow.com/questions/40117582/proof-of-induction-on-pseudocode). It seems remarkably pertinent. – Ami Tavory Oct 18 '16 at 22:33
  • that question was asked by the same user – pm100 Oct 18 '16 at 22:36
  • 1
    Step 1 of the proof: remove all references to `y` – user3386109 Oct 18 '16 at 22:38
  • To clarify some things I edited the question. @user3386109 That would leave me with only a and b (and x but that's pretty much the same thing as a). How would that help to prove the algorithm though? – Labbiqa Oct 18 '16 at 22:49
  • 1
    The algorithm is based on the premise: `B divides A` *if and only if* `B divides (A-B)` for all `A>=B`. My proof of that would be, "Duh!". If you need a more formal proof, then [mathematics.SE](http://math.stackexchange.com/) would be a better choice. BTW, proof by induction can work for this, but `x` and `y` are just distractions, so they should be removed. – user3386109 Oct 18 '16 at 22:56
  • @user3386109 Thanks, I'll try there. Hm, I don't think I'm supposed to use proof by induction since I was asked to use it in the previous question (the one linked in my edit) – Labbiqa Oct 18 '16 at 23:07
  • I hope I'm not missing something obvious but did you mean y = y + b? – EJoshuaS - Stand with Ukraine Oct 19 '16 at 15:18

1 Answers1

2

Try to show the Pre-condition and Post-condition of the algorithm. Pre-condition predicates the initial state before execution and Post-condition predicates the final state after execution.

The algorithm is correct if it can be proved that if the pre-condition is true, the post-condition must be true.

check this out for more details: prove how algorithm works

itproxti
  • 51
  • 1
  • 5