I was asked the following question in an interview:
Is there any way in which Fibonacci series can be generated using only 1 variable ?
Yes, you can used the closed-form expression:
You can calculate the expression using a
double and round the result to the nearest integer. Because of the finite precision of floating point arithmetic this formula will give a wrong answer for large enough n, but I think it will work in the case when the result fits into a Java 32-bit integer.