Search for previous position and two positions back in for loop

-3

Within a for loop, I need to get the previous value of the variable i and the previous value. See, subtracting the variable from -1 and -2 does not work. This is not to get two or a previous position, but to subtract the current value of 1 or 2. What I want is the Fibonacci sequence, but I ask that nobody send me code ready, only if there is any way to get these values of the form as I said I would like to build logic and code myself.

public List<int> fibonacci(int fim)
{
    List<int> lista = new List<int>();

    int r = 0;
    int x = 0;

    for (int i = 0; i < fim; i++ )
    {
        if (i == 0)
        {
            r = 0;
            lista.Add(r);
        }
        else
        {
            if (i == 1)
                r = 1;
            else
                r = r ==>> Aqui que estou apanhando

            lista.Add(r);

            x = r;
        }

    }
    return lista;
}
    
asked by anonymous 12.06.2015 / 17:05

2 answers

2

I solved this. A colleague from another forum has posted me this algorithm:

  

1 - Declare an integer variable with the name of (a), starting at 0;   - Declare an integer variable with the name of (b), starting at 1;
3 - Create a loop with the integer variable (i), starting at 0, until the number   you want to calculate.
4 - Declare an integer variable   temporary (temp) within the loop, and assign the value of the first   variable (a)
5 - Redeclaring the variable (a) with the contents of the variable   (b)
6 - Redeclaring the variable (b) with the contents of the variable (temp)   added to the contents of variable (b)
7 - Display the value of variable (a)

Then I did this:

public List<int> fibonacci(int fim)
        {
            List<int> lista = new List<int>();

            int a = 0;
            int b = 1;

            for (int i = 0; i < fim-1; i++ )
            {
                int x = a;

                a = b;

                b = x + b;

                lista.Add(a);

            }

            return lista;
        }

Also suggestion of @VictorStafusa (two variables, exactly as he suggested).

    
12.06.2015 / 21:02
1

@Maicon is right that you are probably just missing an index.

If you want to do this without indexes, you can create a IEnumerator ". The constructor (say, LookBehindEnumerator ) would accept a Enumerator (in this case, the initial terms of the Fibonacci sequence) and the number of terms to look back (in case two). The constructor is responsible for reading the first N terms of the enumerator and saving it to a member variable (which would be the "history" of the sequence).

The MoveNext would take the next term in the sequence, concatenate the history, discard the history, and return the current history.

The loop that consumes the output of this IEnumerator would not receive a int , but a List<int> , and then your Fibonacci would simply use the list of two elements as a result to put one more term in the same list ( I imagine that if you add the new terms in the list, foreach will run infinitely, inclusive; you would have to have a stop condition or use Fibonacci just as an enumerator.

I do not know how much code I should put in my answer, since you want to do it yourself, but the type signature would be something like:

class LookBehindEnumerator<IEnumerator<T> TEnumerator, int WindowSize> : IEnumerator<List<T>>
    
12.06.2015 / 17:38