Question: Optimizing a recursive procedure

Dear Maple users

I am working on a problem, which basicly is dealing with putting n identical marbles in k different boxes. I need to run through all possible combinations and do something with it. The latter is unimportant. Mathematics say there are binomial(n+k-1,k-1) different combinations. I have already produced a recursive procedure to solve the problem and it is displayed below. I have placed a counter in the procedure to control if the procedures get through all possible combinations, and it does! The comment "#Do something here" should contain some code, but it is not important here, I guess. I actually have a mathematical question as well as a Maple coding question.

The mathematical: I have heard that a recursive procedure sometimes can me transformed into an iterative procedure, which will maybe make it execute faster. I can't find a way to do it iteratively ... it does not seem as easy as the Fibonacci sequence or Faculty. Execution times are probably also dependent on how the compiler of Maple is working.

The Maple implementation: If we need to stay with this recursive procedure, then is there a way to optimize the code? Making it work parallel, etc.?

All suggestions or comments will be appreciated. Here is the code:
A_recursive_procedur.mw

Erik

-------------------------------------------------

 

 

Please Wait...