Can anyone explain me this Fibonacci() method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

25th Apr 2021, 7:35 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
12 Answers
+ 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
25th Apr 2021, 8:07 AM
JaScript
JaScript - avatar
+ 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.
25th Apr 2021, 10:41 AM
JaScript
JaScript - avatar
+ 3
This is a recursive function. There are cases where in this way easier and more elegant solution can be found.
25th Apr 2021, 7:43 AM
JaScript
JaScript - avatar
+ 3
You can use debuger or here in SL Playground build in a print statement to see it what i describe above.
25th Apr 2021, 8:25 AM
JaScript
JaScript - avatar
+ 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
25th Apr 2021, 9:32 AM
JaScript
JaScript - avatar
+ 3
I am glad to have helped. Continue to have fun with it.
25th Apr 2021, 10:53 AM
JaScript
JaScript - avatar
+ 2
Fibonacci series is 1,1,2,3,5,8,13,21......and so on....The code listed gives the desired series..
26th Apr 2021, 3:00 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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?
25th Apr 2021, 7:46 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
+ 1
Okay I think I got it 😅 it's a strange sequence
25th Apr 2021, 8:23 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
+ 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.
25th Apr 2021, 8:45 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
+ 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?
25th Apr 2021, 9:59 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
+ 1
Thank you a lot, you gave me motivation to stick on it XD.
25th Apr 2021, 10:44 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar