3
$\begingroup$

In the following, I want to know b[1000], but it is taking a very long time even for b[30].

Please help

a[0]=1.
a[n_]:=a[n - 1] + 1./ Sqrt[a[n - 1]];
b[n_] := (a[n])^3/n^2;
b[30]
$\endgroup$
  • $\begingroup$ Every time you call b it is in turn calling a recursively. Try the simple change of a[n_]:=a[n]= this should run quite quickly even for large n. Though be warned that it will use up memory, since you are trading speed for storage here. See this for more details. reference.wolfram.com/language/tutorial/… $\endgroup$ – enano9314 Aug 27 '18 at 4:32
  • 1
    $\begingroup$ @enano9314, Thanks a lot for your help $\endgroup$ – Epsilon Aug 27 '18 at 4:37
7
$\begingroup$

Use memoization:

a[0] = 1.;
a[n_] := a[n] = a[n - 1] + 1./ Sqrt[a[n - 1]]
b[n_] := (a[n])^3/n^2;
b[1000] //AbsoluteTiming

{0.052607, 2.2588}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.