# How to define a nested function like this?

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

$f(1)=1$

$f(2)=\frac{1}{1+2}$

$f(3)=\frac{1}{1+\frac{2}{1+3}}$

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.

You can use Fold instead:

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


$\frac{1}{1+\frac{2}{1+3}}$

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

f /@ Range // ListLinePlot[#, PlotRange -> All] & • Seems the Limit calc isn't trivial with this formulation – Dr. belisarius Nov 19 '14 at 18:59
• Yup, I'm also struggling with it... I only posted as an answer because the question asks how to define such function... – Aisamu Nov 19 '14 at 19:04
• 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! – Aisamu Nov 19 '14 at 19:14
• You could take off the condition _Integer and then wrap with Quiet – M.R. Nov 19 '14 at 19:16
• See formula (16) here mathworld.wolfram.com/ContinuedFractionConstant.html – Dr. belisarius Nov 19 '14 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. >>

ContinuedFractionK[k,1,{k,1,Infinity}]


We can easily approximate the limit though:

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

0.525135


## 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 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, f' == 1}, f[x], x];

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

limit = Limit[E1/E2, x -> Infinity] // FullSimplify

Sqrt[2/(E π)]/Erfc[1/Sqrt] - 1

N[limit]

0.525135

• Very nice, now that I look at it lo these many months later. – Daniel Lichtblau Mar 30 '15 at 22:38
f[n_] := Module[{i = 1}, Nest[(n - i++)/(a + #) &, n, n - 1]]