Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest / most general way to eliminate recursion, in general, is to use an auxiliary stack -- instead of making the recursive calls, you push their arguments into the stack, and iterate. When you need the result of the recursive call in order to proceed, again in the general case, that's a tad more complicated because you're also going to have to be able to push a "continuation request" (that will come off the auxiliary stack when the results are known); however, in this case, since all you're doing with all the recursive call results is a summation, it's enough to keep an accumulator and, every time you get a number result instead of a need to do more call, add it to the accumulator.</p> <p>However, this, per se, is <em>not</em> fixed space, since that stack will grow. So another helpful idea is: since this is a pure function (no side effects), any time you find yourself having computed the function's value for a certain set of arguments, you can <a href="http://en.wikipedia.org/wiki/Memoization" rel="noreferrer">memoize</a> the arguments-result correspondence. This will limit the number of calls. Another conceptual approach that leads to much the same computations is <a href="http://en.wikipedia.org/wiki/Dynamic_programming" rel="noreferrer">dynamic programming</a> [[aka DP]], though with DP you often work bottom-up "preparing results to be memoized", so to speak, rather than starting with a recursion and working to eliminate it.</p> <p>Take bottom-up DP on this function, for example. You know you'll repeatedly end up with "how many ways to make change for amount X with just the smallest coin" (as you whittle things down to X with various coin combinations from the original <code>amount</code>), so you start computing those <code>amount</code> values with a simple iteration (f(X) = <code>X</code>/<code>value</code> if <code>X</code> is exactly divisible by the smallest-coin value <code>value</code>, else <code>0</code>; here, <code>value</code> is 1, so f(X)=X for all X>0). Now you continue by computing a new function g(X), ways to make change for X with the <em>two</em> smallest coins: again a simple iteration for increasing X, with g(x) = f(X) + g(X - <code>value</code>) for the <code>value</code> of the second-smallest coin (it will be a simple iteration because by the time you're computing g(X) you've already computed and stored f(X) <em>and</em> all g(Y) for Y &lt; X -- of course, g(X) = 0 for all X &lt;= 0). And again for h(X), ways to make change for X with the <em>three</em> smallest coins -- h(X) = g(X) + g(X-<code>value</code>) as above -- and from now on you won't need f(X) any more, so you can reuse <em>that</em> space. All told, this would need space <code>2 * amount</code> -- not "fixed space" yet, but, getting closer...</p> <p>To make the final leap to "fixed space", ask yourself: do you need to keep around <em>all</em> values of two arrays at each step (the one you last computed and the one you're currently computing), or, only <em>some</em> of those values, by rearranging your looping a little bit...?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload