Can I turn this non-linear iterative function into a non-iterative function?

54 Views Asked by At

My function looks like this:

function fn(startValue, iterations){
    var result = startValue;
    while(iterations--)
    {   // Loop the # of iterations
        result += 1 / (1 + result);
    }
    return result;
}

If startValue = 0 and iterations = 3 then the resulting math would be...

startValue = 1 / (1 + startValue);
startValue = 1 / (1 + startValue);
startValue = 1 / (1 + startValue);

And we end up with startValue equaling 1.9

How can I get that same result without looping?

http://jsfiddle.net/LgJZP/

1

There are 1 best solutions below

0
On

Look at the result. The numerator and denominator are Fibonacci numbers just starting at different places.