Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
I have the following recursive function:
C#
public int Fibonacci(int number)
      {
          if (number == 0)
              return 0;
          else if (number == 1)
              return 1;
          else
          {
              return Fibonacci(number - 2) + Fibonacci(number - 1);
          }

      }

My questions are:

why the function returns 0 if i return 0 in the else condition,
why the function returns the value multiplied by 2 if I return 2 in the else condition.
So the test scenario will be:

Fibonacci(5) // when (else return 0) | Result: 0
Fibonacci(5) // when (else return 1) | Result: 5 // Correct
Fibonacci(5) // when (else return 2) | Result: 10


I don't understand why, and when the multiplication is happening.

Any explanation?

What I have tried:

C#
public int Fibonacci(int number)
      {
          if (number == 0)
              return 0;
          else if (number == 1)
              return 1;
          else
          {
              return Fibonacci(number - 2) + Fibonacci(number - 1);
          }

      }
Posted
Updated 26-Feb-16 5:40am
v3
Comments
Jochen Arndt 26-Feb-16 10:50am    
There is no multiplication. It happens by the recursive call and adding the results for F(n-1) and F(n-2).

However, your code is not a valid Fibonacci implementation. F(5) is 5 and not 13. See https://en.wikipedia.org/wiki/Fibonacci_number.
Keymayker 26-Feb-16 10:58am    
Okey i implemented correct Fibonacci implementation, but again i have the same issue, and question
Jochen Arndt 26-Feb-16 11:29am    
What issue?
By definition F(0) = 0, F(1) = 1, and F(2) = 1.
Why would you want to return anything else?
You can do so, off course, but then it won't be Fibonacci.

0 and 1 provide a way for your recursive method to end: if the tests weren't there then it would just keep on calling itself until it ran out of stack space and crashed!
Think about what it does, and try running it in the debugger. Pit a breakpoint on the first call to the method, and step into it, and watch exactly what happens as you step through. You'll soon start to see what's happening, and why the two tests are essential!
 
Share this answer
 
This is not your code!
So first thing to do is to read a reference about the Fibonacci sequence Fibonacci number - Wikipedia, the free encyclopedia[^].

Quote:
why the function returns 0 if i return 0
This is part of the definition F0= 0

Quote:
in the else condition,
why the function returns the value multiplied by 2 if I return 2 in the else condition.
Jumping from 1 to 2 is not necessary a multiplication. read source code more carefully.

Quote:
So the test scenario will be:

Fibonacci(5) // when (else return 0) | Result: 0
Fibonacci(5) // when (else return 1) | Result: 5 // Correct
Fibonacci(5) // when (else return 2) | Result: 10
No!
Check the reference to get the right answers.

Quote:
I don't understand why, and when the multiplication is happening.

Any explanation?
Because there is no multiplication!
Read again the source code.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 
v2
Comments
Snesh Prajapati 26-Feb-16 19:46pm    
+1 ...well said.
Patrice T 26-Feb-16 19:53pm    
Thank you
I guess you want to say +5 :)
Snesh Prajapati 26-Feb-16 20:38pm    
Most Welcome...actually I wanted to say "ditto" [ https://www.quora.com/What-does-+1-mean-when-someone-uses-it-in-an-online-forum-or-a-question-website-like-this ] ......and obviously the answer deserve rating 5.
Patrice T 26-Feb-16 21:31pm    
Thank you
RazorRazorRazor 29-Feb-16 7:10am    
ppolymorphe so if you read the question carefully, it is not relevant what implementation of fibonacci is or isn't or what eva... If someone have a problem, you should be kind and be of some help, so, from this types of answers I'm sick, and from time to time I vomit.
We know that you are smart enough to share atoms of your brain about what is wrong, but I think You are what is wrong.

So, lets return to the question:

why the function returns 0 if i return 0 in the else condition,
why the function returns the value multiplied by 2 if I return 2 in the else condition.

Can you be smart enough to answer this ? Or we will criticize the grammar of the question, or maybe there is some letter wrong.. or I don't know.

However, here is C# Fiddle, so that you can express yourself by delevering Right answer : https://dotnetfiddle.net/uzJuFY

Btw: "This is not your code!" - Who is the owner ? Please tell us, enlighten us, please, please...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900