Join Stack Overflow to learn, share knowledge, and build your career. 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 bit integer.
If you're curious as to how it works, it's really quite simple. The one variable is actually divided into two parts and those two parts maintain the individual values for the Fibonacci sequence. It's still technically one variable, we've just imposed some extra structure on top of it to achieve our ends.
Yes, but you still need to remember 2 values. You could take a bit variable and use it as 2 bit vars. The first example I could think of, using double recursion that leads to an exponential complexity, not recommended:. After the initial 1 1 , it is in theory possible to generate one value from the previous one until machine precision comes around to bite you via:. This prints as seen on ideone.
This uses only one explicit variable, and it's essentially a linear non-recursive algorithm. It needs to be said that this is an abuse of String , though.
Email Sign Up or sign in with Google. Fibonacci using 1 variable Ask 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? Mark Byers k GoodSp33d 5, 3 26 That's what you want? You are probably going to have to be more specific. What is the purpose of this variable? An initial value, or the nth number in the series?
Yes, you can used the closed-form expression: An explanation of this formula is too basic for mathoverflow. Up to a point, yes though in C, you could convert it to Java - it would look much uglier.
Why would you want to? Aamir, just use a struct with as many fields as you want, or even an array of two ints.
That's still technically declaring one variable: Upvoted as it work, please explain your code. Bart Kiers k 25 I wouldn't know in what production code one needs to calculate the Fibonacci sequence! Besides, did the interviewer said you couldn't make use of recursion? If so, please include that in your original question.
I would say a recursive version uses at least one variable for each level of recursion not to mention all the other registers stacked. Seems more practical to just use three variables, though. My answer doesn't use recursion. Your answer has also twisted the definition of 'one variable'. Multiplexing two values into a block of memory that you happen to have only have one explicit pointer to isn't precisely 'one' variable, any more than an array is 'one' variable.
But given how tricksy the question is in the first place, perhaps your solution would have been a good place to start a conversation.
What about the whole stack of variables the recursive solution uses ;]? Alex, Using recusion also twists the defintion of one variable as it keeps the value of the Left Hand Side fib while it calculates the Right Hand Side fib value.
There is actually a saved value for each level of recusion. The answer is "yes", but maybe you could be more specific. The first example I could think of, using double recursion that leads to an exponential complexity, not recommended: G B 1, 1 16 After the initial 1 1 , it is in theory possible to generate one value from the previous one until machine precision comes around to bite you via: You can always do something like this: I'm going to vote this up just because it gave me a headache: I believe others have suggested this, and I haven't carefully examined your answer to see if it's essentially the same, but basically the "one variable" notion is abused to essentially store multiple things.
I think someone mentioned two bit numbers in a bit variable, etc. So this is evil, but: Andy Holt 2 6. Except you need to add an extra WriteLine outside the loop to write the first "1" since the series usually begins with 1, 1, 2, 3, Yep, you could hard code that in for properness. Also, I guess the index variable "i" also counts as a variable, so that's cheating.
You could however use a while loop instead and test that "fib" is lower than a certain value. Baby Groot 3, 10 40 I count 4 variables 3 if we ignore args. You have two different variables called i. Each for loop declares a different one, visible only within that loop.
I was posting the same codeMore...