Mathematica: Recursive expression

  • Context: Mathematica 
  • Thread starter Thread starter Niles
  • Start date Start date
  • Tags Tags
    Expression Mathematica
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
Niles
Messages
1,834
Reaction score
0
Hi

I am trying to find a smart way to write the following fraction,

$$
F = \frac{a_1}{1+\frac{a_2}{1+\frac{a_3}{1+a_4}}}
$$

Here we can just take [itex]a_n= n[/itex] for simplicity. My fraction is in principle infinite, but I am trying to construct a function which can find [itex]F[/itex] for a given [itex]n[/itex] recursively. I haven't had much success. So far I have

f[n_] := n;
f[n - 1]/(1 + f[n])

which is the last term for a given [itex]n[/itex]. For do I propagate all the way up to [itex]a_1[/itex] then?
 
Physics news on Phys.org
Code:
f[1] = Subscript[a, 1];

f[n_] := (f[n - 1]) /.Subscript[a, n - 1] :> Subscript[a, n - 1]/(1 + Subscript[a, n])
Does that work?