I'm relatively new to Mathematica, and I'm trying to define a function f(k) that would do the following:
For any positive integer $k$, a finite sequence $a_i$ of fractions $\frac{x_i}{y_i}$ is defined by: $$a_1 = \frac{1}{k}\\ a_i = \frac{x_{i-1}+1}{y_{i-1}-1}$$ When $a_i$ reaches some integer $n$, the sequence stops. (That is, when $y_i=1$.) Define $f(k) = n$.
I have the following function written:
f[k_, sofar_] = Module[{num, result},
result =
If[sofar == 0,1/k,((num = Numerator[sofar]) + 1)/(num/sofar - 1)
];
If[IntegerQ[result],result,f[k,result]]
];
f[k_] = f[k, 0]
But I get the result {ComplexInfinity,List}
and I don't understand why. What am I doing wrong?