-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.
– ramaral
Put the code as you are doing, sometimes it is some error in logic pro
-1
and-2
not work– Maicon Carraro
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.
– pnet
Why don’t you declare two variables
anterior
andanteriorDoAnterior
?– Victor Stafusa
From which point do you start the loop? From scratch?
– Felipe Avelar
from 0, I will edit the question and post my code.
– pnet
The suggestion of @Victorstafusa I find very viable.
– pnet