For example, I want to calculate the limit of $\frac{1}{1+\frac{2}{1+\frac{3}{1+\ldots}}}$. So I want to define:




Then I calculate the $n\to\infty$ limit of $f(n)$.

However, I do not know how to define such a funcion in Mathematica. It seems we cannot change the parameter in Nest.


3 Answers 3


You can use Fold instead:

f[n_Integer] := Fold[#2/(1 + #) &, n, Reverse@Range[n - 1]]


It not very useful analytically, but it allows you to invoke the CPU gods:

f /@ Range[50] // ListLinePlot[#, PlotRange -> All] &


  • 1
    $\begingroup$ Seems the Limit calc isn't trivial with this formulation $\endgroup$ Nov 19, 2014 at 18:59
  • $\begingroup$ Yup, I'm also struggling with it... I only posted as an answer because the question asks how to define such function... $\endgroup$
    – Aisamu
    Nov 19, 2014 at 19:04
  • $\begingroup$ I've tried Analytic->True but it still gives me warnings of the form "Range specification in Range[-1+n] does not have appropriate bounds. >>", and an answer that is different from f@n_lim! $\endgroup$
    – Aisamu
    Nov 19, 2014 at 19:14
  • $\begingroup$ You could take off the condition _Integer and then wrap with Quiet $\endgroup$
    – M.R.
    Nov 19, 2014 at 19:16
  • 1
    $\begingroup$ See formula (16) here mathworld.wolfram.com/ContinuedFractionConstant.html $\endgroup$ Nov 19, 2014 at 19:19

We can express f[n] as a continued fraction:

f[n] == ContinuedFractionK[k, 1, {k, 1, n}]

Now unfortunately, Mathematica says the following diverges, when it really doesn't:

ContinuedFractionK[k, 1, {k, 1, Infinity}]
  ContinuedFractionK::div: The continued fraction does not converge. >>


We can easily approximate the limit though:

ContinuedFractionK[n, 1., {n, 1, 10000}]

Edit: a way to find the closed form of the limit

The value of your desired limit is $$ \frac{\sqrt{2/(e \pi)}}{\text{erfc}(1/\sqrt{2})} - 1 \approx 0.525135. $$

Here's how I found it.

Since ContinuedFractionK[k, 1, {k, 1, n}] returns enter image description here

if we can solve these recurrence relations, we can potentially find the limit.

Now I was unable to find a way to solve these, but what I was able to do was find the closed form of their exponential generating functions. Taking the limit of the quotient of these e.g.f's will give the same result.

Now both recurrences look like

a[n+2] - a[n+1] - n a[n] - 2a[n] == 0

with some initial conditions. If these are Taylor series coefficients to a function f[x], then a[n+2] corresponds to f''[x], a[n+1] corresponds to f'[x], n a[n] corresponds to x f'[x], and a[n] corresponds to f[x]. This gives us an ODE that f[x] satisfies. Solving it (with the appropriate initial conditions) will give us our e.g.f's.

(* e.g.f of numerator *)
E1 = DSolveValue[{f''[x] - (x + 1)f'[x] - 2f[x] == 0, f[0] == 0, f'[0] == 1}, f[x], x];

(* e.g.f of denominator *)
E2 = DSolveValue[{f''[x] - (x + 1)f'[x] - 2f[x] == 0, f[0] == 1, f'[0] == 1}, f[x], x];

limit = Limit[E1/E2, x -> Infinity] // FullSimplify
Sqrt[2/(E π)]/Erfc[1/Sqrt[2]] - 1
  • 1
    $\begingroup$ Very nice, now that I look at it lo these many months later. $\endgroup$ Mar 30, 2015 at 22:38
f[n_] := Module[{i = 1}, Nest[(n - i++)/(a + #) &, n, n - 1]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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