GoodSp33d - 1 year ago 82

Java Question

I was asked the following question in an interview:

Is there any way in which Fibonacci series can be generated using only 1 variable ?

I didn't know what to answer. What should I have said?

Answer Source

Yes, you can used the closed-form expression:

where

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.