0

(I took this example from another question)

f[0] = 0;
f[n_] := f[n] = DigitCount[n, 10, 1] + f[n - 1];
f[1016]

I notice that this example defines f(n) in a different way from the one given by the documentation, which would be (the second line changes):

f[0] = 0;
f[n_] := DigitCount[n, 10, 1] + f[n - 1];
f[1016]

Is there a difference between these two ways of defining the function? When I run it, the output is the same, implying there's no difference; however I'd like to be sure.

Allure
  • 625
  • 4
  • 11
  • 1
    The first one has memory, the second one doesn’t. So when you use the first definition and do f[3] it does the math and also stores the value. The next time you call it, it just pulls the precomputed value. Trades storage for speed. A slick trick! – MikeY Dec 21 '18 at 03:25
  • @MikeY suggest writing that as an answer :) – Allure Dec 21 '18 at 03:29
  • Nevermind: this is a better one. – march Dec 21 '18 at 03:45

0 Answers0