A pretty good puzzle, perhaps best solved using FoldList
. :-)
Note: you should not start user Symbol names with capital letters as these may conflict with internal functions.
I use the carried expression of FoldList
to keep track of three elements that are (re)used at each step. These are {sum, a0, c}
:
sum: the denominator apart from the current $b$ value.
a0: The $a$ value from the previous step.
c: the $c$ value from the previous step.
This triplet is then fed, along with the "current" $(a, b)$ pair to the function next
.
At the end we take the third element of every list (the $c$ values), and drop the seed list {0, 0, 1}
, done concisely with Part
using [[2;;, 3]]
.
aList = {"a1", "a2", "a3", "a4"}; (* example data *)
bList = {"b1", "b2", "b3", "b4"}; (* example data *)
next[{sum_, a0_, c_}, {a_, b_}] := With[{x = sum - a0/c}, {x, a, f[a/(b + x)]}]
FoldList[next, {0, 0, 1}, {aList, bList}\[Transpose]][[2 ;;, 3]] // Column

Addendum
If you wish to carry out this operation with arbitrary functions rather than a hard-coded f
you might rewrite next
to use a SubValues pattern:
next[f_][{sum_, a0_, c_}, {a_, b_}] := With[{x = sum - a0/c}, {x, a, f[a/(b + x)]}]
To use function func
now in place of f
:
FoldList[next[func], {0, 0, 1}, {aList, bList}\[Transpose]][[2 ;;, 3]]
Alternatively, and especially if your f
is compilable you may wish to do something like this:
nextC[f_] :=
Compile[{{old, _Real, 1}, {new, _Real, 1}},
With[{x = old[[1]] - old[[2]]/old[[3]]}, {x, new[[1]], f[new[[1]]/(new[[2]] + x)]}]
]
This is an order of magnitude faster than the simple version on my test:
aList = RandomReal[99, 150000];
bList = RandomReal[77, 150000];
FoldList[next[Mod[#, 7] &], {0`, 0`, 1`}, {aList, bList}\[Transpose]][[2 ;;, 3]] //
Timing // First
FoldList[nextC[Mod[#, 7] &], {0`, 0`, 1`}, {aList, bList}\[Transpose]][[2 ;;, 3]] //
Timing // First
0.624
0.062
Note my use of the backtick (`
) to declare the seed values as machine-precision Real numbers, to match the Compile
function's specification. See this for more information.