-2

My code is supposed to calculate the 100th element of the sequence $x_0=1 ; x_i=\dfrac{x_{i-1}+1}{x_{i-1}+2}, i=1,2, \ldots$

I wrote iterative and recursive functions, but the results are not equal. Is it due to the lost of decimals?

Here is my driver code. The data from the file is i=100.

int main()
{
    int i;

    ifstream f ("data.txt");
    f >> i;

    double x_0= 1.00;

    double x_100 = l(x_0, i);

    ofstream g ("results.txt", ios::app);
    g <<"\n100th element (by looping): " << x_100;

    x_100 = r(x_0);
    g <<"\n100th element (by recursion): " << x_100;

 
   return 0;
}

l() is iterative function, r() is recursive function


double l(double x, int i)
{
    for (int j = 0; j<i ; j++){
            x = (x + 1)/(x+2);
    }
    return x;

}

double r(double x)
{
    if (x == 0)
        return 1;
    else
        return (r(x-1) + 1) / (r(x-1) + 2);
}

Here are the results

100th element (by looping): 0.618034
100th element (by recursion): 0.666667
  • 4
    Yes, if the code is not written to produce identical observable behaviour. – user4581301 Nov 07 '22 at 15:31
  • 2
    `r` can't be right. It doesn't mention `100` anywhere. It just runs the recursion until the sequence converges all the way to `0`, so nested calls always return `1`, and the top-level call returns `(1 + 1) / (1 + 2) == 2/3 == 0.666666`. This is what you observe. – Igor Tandetnik Nov 07 '22 at 15:32
  • 2
    any recursive algorithm can be written as an iterative one : any obeserved differences thus are the result of bugs. – Pepijn Kramer Nov 07 '22 at 15:33
  • ...Neither does `l(x, i)` contain `100`. `l(x, i)` seems to calculate the `i`th element of the sequence not the 100th (allthough `i` is set to 100 here). That parameter is missing entirely on `r(x)`. – Lukas-T Nov 07 '22 at 15:36
  • It looks like you're confusing the elements of the sequence with their indices; the initial value for `x` is 1.0, but you're comparing it to (the index) 0 to determine when to stop. – molbdnilo Nov 07 '22 at 15:47
  • I have fixed the bug. I realized I was passing the wrong argument as @IgorTandetnik remarked. The problem now is that it is slow. For the 20 th element, function r() took 5 seconds! I tried 100 th but it simply does not finish the calculation. – Maroon Racoon Nov 07 '22 at 16:54
  • It's running in exponential time, since you compute the same value `r(x-1)` twice (and then each of those calls in turn makes 2 recursive calls, and those each make another two, ...) Call `r(x-1)` once, save the result in a local variable, use that variable when computing division. – Igor Tandetnik Nov 07 '22 at 18:38

1 Answers1

6

I the recursive function you do

(r(x-1) + 1) / (r(x-1) + 2)

With x == 1.0 that's equal to

(r(1-1) + 1) / (r(1-1) + 2)

That's of course equal to

(r(0) + 1) / (r(0) + 2)

And since r(0) will return 1 that equation is

(1.0 + 1) / (1.0 + 2)

There's no further recursion. The result is 2.0 / 3.0 which is 0.66667.

The iterative function l on the other hand will do 100 iterations where each iteration will change the value of x, making it even smaller and smaller.

The functions simply does different things, leading to different results.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621