Fetch previous position and two positions back in for loop

Asked

Viewed 159 times

-3

In a noose for, i need to take the previous value of the variable i and the value before the previous one. See, subtracting the variable from -1 and -2 does not work. This is not taking two or a previous position, but subtracting 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 some way to get these values the way I said. I’d like to assemble the logic and the 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;
}
  • You’ll have to explain why subtracting 1 and 2 doesn’t work.

  • 4

    Put the code as you are doing, sometimes it is some error in logic pro -1 and -2 not work

  • is sequence of Fibonacci. Type at position 3 of I would have I -1 = 2 + I - 2 = 1, this would give 3 and should be 2, because Fibonacci is: 0 1 1 2 3 5 8 13.... See that I-1 and I-2 wouldn’t work.

  • 2

    Why don’t you declare two variables anterior and anteriorDoAnterior?

  • From which point do you start the loop? From scratch?

  • from 0, I will edit the question and post my code.

  • 1

    The suggestion of @Victorstafusa I find very viable.

Show 2 more comments

2 answers

2


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

1 - Declare an integer variable with the name of (a), starting at 0;
2 - Declare an integer variable named (b) starting at 1;
3 - Create a loop with the integer variable (i), starting at 0, up to the number end you want to calculate.
4 - Declare an integer variable temporary (temp) inside the loop, and assign the value of the first variable (a).
5 - Redeclare variable (a) with variable content (b)
6 - Redeclare variable (b) with variable content (temp) added to the variable content (b)
7 - Display variable value (a)

So I did that:

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;
        }

Suggestion also from @Victorstafusa (two variables, exactly as he suggested).

  • Mark this answer as sure if you solved your problem.

  • I can only schedule after two days, because the post was mine.

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 builder (say, of LookBehindEnumerator) would accept a Enumerator (in this case, the initial terms of the Fibonacci sequence) and the number of terms to look back on (in case two). The constructor is responsible for reading the first N terms of the enumerator and storing it in a member variable (which would be the "history" of the sequence).

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

The loop that will consume the output of this IEnumerator would receive not 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 on the same list (I imagine if you add the new terms in the list, the foreach will rotate infinitely, including; you would have to have a stop condition or use Fibonacci only as an enumerator.

I don’t know how much code I should put in my answer, since you want to do it yourself, but the signature of the kind would be something like:

class LookBehindEnumerator<IEnumerator<T> TEnumerator, int WindowSize> : IEnumerator<List<T>>
  • (I was too vague?)

  • Not necessarily. But I think that implementing the Fibonacci algorithm should not be so complex, because any company today asks as a test on paper the implementation of this algorithm. I’m just training faster solutions. I realized over the course of my walk that I’ve spent too much time on simple things and so I’m studying to have things more present in my mind.

  • I didn’t understand the two downvotes I received. What’s wrong with my question? As I’ve always said, there are some guys here who think they are. Otherwise it’s no good for them. I just asked you not to send me codes, that’s what breaks the rules?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.