Need help proving a recursive function works

In summary: You assume that the routine works for z=n. You use the inductive hypothesis to show that it works for z=n+1.
  • #1
SpiffyEh
194
0

Homework Statement



Prove the correctness of the following recursive algorithm to multiply two
natural numbers, for all integer constants c ≥ 2.
function multiply(y, z)
comment Return the product yz.
1. if z = 0 then return(0) else
2. return(multiply(cy,[ z/c]) + y · (z mod c))

Homework Equations





The Attempt at a Solution


I know I have to use an inductive proof
Base case: z=0
the vaule returned by line 1 is 0 which is correct
Inductive Step:
Inductive Hypothesis: The algorithm works for z =n
Show: the algorithm works for z=n+1

I know I have to do both even and odd due to the mod in the function but I'm confused because I don't know what y is or c and I'm not sure where to go with it. Can someone please help me?
 
Last edited:
Physics news on Phys.org
  • #2
You're told that c is an integer greater than or equal to 2 and that y is, like z, a natural number. I take it z/c is supposed to be an integer division, i.e. you throw away the fractional part. It looks like you need to use http://mathcircle.berkeley.edu/BMC4/Handouts/induct/node6.html since z/c isn't typically equal to z-1.

You might try expressing z as z=qc+r for some non-negative integers q and r where 0≤r≤c-1.
 
Last edited by a moderator:
  • #3
I'm lost, I've never heard of string induction so I'm not sure what to do there. I don't understand the z=qc+r either, the only way we've done insuctive proofs is the way i started in the problem
 
  • #4
With weak induction, you assume that p(k) is true and prove that p(k+1) is true. You make no assumptions about the validity of p(k-2), p(k-3), ... , p(1). With strong induction, you assume that p(k), p(k-1), ..., p(1) are all true for the induction step.

Look at a specific case where c=2 and say you wanted to show multiply(1,11) gives the right answer. You have [z/c]=5 and yc=2, so the function will give the correct answer if multiply(2,5) returns the right answer. If you were using weak induction, you wouldn't be able to assume multiply(2,5) returns the right answer; you'd only know that multiply(something,10) was correct because the assumption is it works for z-1=10. With strong induction, you're fine assuming the routine works all the way back to the base case z=0.

When you say z=qc+r, you're just dividing z by c. The quotient is q and the remainder is r. For instance, take z=11 and c=4. If you divide 11 by 4, you get a quotient of 2 and a remainder of 3, i.e. 11=2(4)+3. The reason for writing it this way is that [z/c]=q and (z mod c)=r.
 
Last edited:
  • #5
Sorry for the late reply. I had a chance to go see the teacher for the course and this in the information she gave me.

You assumed the algorithm works for all z<=n and you want to prove that it works for n. We know that yn=(cyn)/c=(cy)(n/c). Can you put the division, n/c, in terms of the floor of n/c and n%c? If you can do that then you can show yn=cy*floor(n/c)+y(n mod c), which is what line 2 of the algorithm should return. Thus you have shown that the math works.

Then all you have to do is show that line 2 of the algorithm, multiply(cy,floor(z/c))+y(z mod c) really returns cy*floor(n/c)+y(n mod c). Does the recursive call return the correct value?

So, I have somewhat of a starting point but I'm still confused about what she said
 
  • #6
SpiffyEh said:
You assumed the algorithm works for all z<=n and you want to prove that it works for n.
This is just strong induction as opposed to weak induction.

To prove the routine works, you have to show two things: (1) the math it's based on works and (2) the implementation is correct.
We know that yn=(cyn)/c=(cy)(n/c). Can you put the division, n/c, in terms of the floor of n/c and n%c? If you can do that then you can show yn=cy*floor(n/c)+y(n mod c), which is what line 2 of the algorithm should return. Thus you have shown that the math works.
This might help with (1). It looked a bit terse, in my opinion, if you haven't seen it before.

http://en.wikipedia.org/wiki/Division_algorithm

In your proof, it may be enough to just assert that "By the division algorithm, we know we can write blah blah blah."
Then all you have to do is show that line 2 of the algorithm, multiply(cy,floor(z/c))+y(z mod c) really returns cy*floor(n/c)+y(n mod c). Does the recursive call return the correct value?
Here, you're proving that the implementation is correct. It's where the induction part of the proof comes in.
 

Related to Need help proving a recursive function works

1. What is a recursive function?

A recursive function is a function that calls itself repeatedly until a specific condition is met. This allows for solving complex problems by breaking them down into smaller, more manageable sub-problems.

2. How do you prove that a recursive function works?

To prove that a recursive function works, you need to show that it correctly solves the problem at hand and terminates at the desired condition. This can be done by using mathematical induction, where you prove that the function works for the base case and then assume it works for a general case and prove it for the next case.

3. What is the base case in a recursive function?

The base case in a recursive function is the condition that stops the function from calling itself and allows it to return a value. This is usually the simplest form of the problem that can be solved without further recursion.

4. Can a recursive function cause an infinite loop?

Yes, if the base case is not properly defined or if the recursive call does not lead to the base case, a recursive function can cause an infinite loop. This can be avoided by carefully defining the base case and ensuring that the recursive call brings the problem closer to the base case.

5. Are there any advantages to using recursive functions?

Recursive functions can be very useful for solving problems that can be broken down into smaller sub-problems. They can also make code more concise and elegant. However, they may not be the most efficient solution for all problems and can be difficult to debug.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
972
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
911
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top