Can anyone explain me this Fibonacci() method? | Sololearn: Learn to code for FREE!

+2

Can anyone explain me this Fibonacci() method?

public void Fibonacci(int x){ if(x<=1){ return 1; }else return Fibonacci(x-1) + Fibonacci(x-2); } I came across this in the challenges of c# coding.

4/25/2021 7:35:24 AM

Soreanu Stefan Alexandru

12 Answers

New Answer

+5

The fibonacci series is a series in which each number is the sum of the previous two numbers. In your code is a mistake because f(0) =0 and f(1) =1. Right should be: f(3) = f(2) + f(1) = f(1) + f(0) + f(1) = 1 + 0 + 1 = 2 Your code make: f(3) = f(2) + f(1) = f(1) + f(1) = 1 + 1 = 2

+5

This course here is for the beginning only. What to do next depends on your preferences or perhaps better your knowledge from other fields. You can specialize in the direction of games, AI, business statistics mathematics, visualization etc. nowadays as a software developer. In any case, continue to deepen your language and algorithms skills.

+3

This is a recursive function. There are cases where in this way easier and more elegant solution can be found.

+3

You can use debuger or here in SL Playground build in a print statement to see it what i describe above.

+3

Debuger on PC i.e. Visual Studio. Here on SL Playground some prints between. Please see thid example: https://code.sololearn.com/cFzELER948iv/?ref=app

+3

I am glad to have helped. Continue to have fun with it.

+2

Fibonacci series is 1,1,2,3,5,8,13,21......and so on....The code listed gives the desired series..

+1

But how does it work properly, I don't get it, let's say I give it the number 4 what does it do then with that 4?

+1

Okay I think I got it 😅 it's a strange sequence

+1

It simply makes the sum of the last 2 numbers repeatedly till the numbers become 1 or 0. I saw a table of the first 12 fibonacci numbers. That's the logic of this method I think.

+1

Yea I definitely got it, thank you a lot <3 Btw, do you have any tips for me on what can I do once I finished the c# course?

+1

Thank you a lot, you gave me motivation to stick on it XD.