so my work was to convert this method:
long fib(long n) {
long i = -1, a = 0, b = 1;
while (++i != n)
b = a + (a = b);
return a;
}
into another iterative fibonacci method that still does the EXACT same just with the difference that the new one is only allowed to contain simple instructions/assignments (but in this context the while loop is also allowed).
So I made this method:
public static long fib2(long n){
long i = 0;
long a = 0;
long b = 1;
while (i != n){
long oldA = a;
a = b;
b = oldA + a;
++i;
}
return a;
}
My question now is: Is it possible to simplify the method and still only keep all the old variables from the 1st method (n, i, a, b) or do I HAVE TO create a new variable (in my case oldA) therefore. Because if it would be possible to do it only with the old variables then that would be better but I don't know how.
I'm asking all this because in the next step I need to find the loop invariant for this method and I think its easier if I just have the given variables and don't add new ones. (I think not sure tho).
Thx in advance.