Fibonacci Series | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Fibonacci Series

Last night I was studying the classes of Ruby and saw a question about the fibonacci series using recursive function, but didn't understand very well how it works. Can anybody try to explain please? Thanks! :) def fib (num) if num <2 num=1 else fib (num-1) +fib (num-2) end end puts fib (4) #output 3 PD: Sorry if my english isn't good, I'm from Chile and here we speak Spanish hahaha

6th Nov 2016, 8:44 PM
Matias Schiaffino
Matias Schiaffino - avatar
10 Answers
+ 7
You get the fibonacci numbers by starting with 1 and 1, and then generating the next element by adding the previous two. In other words, fibs = 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Or, a bit more mathsy: fib(0) = 1 fib(1) = 1 fib(2) = fib(0) + fib(1) = 1 + 1 = 2 fib(3) = fib(1) + fib(2) = 1 + 2 = 3 fib(4) = fib(2) + fib(3) = 2 + 3 = 5 ... fib(n) = fib(n-2) + fib(n-1) The code you have there is pretty much the mathematical definition - just get the previous two fibonacci numbers and add them together. If num<2, that is to say we want fib(0) or fib(1), we return 1 instead. If you want a better idea of how the recursive function calls itself, check this piece of code: https://code.sololearn.com/crl922SJGQ5o/#rb
6th Nov 2016, 8:55 PM
Schindlabua
Schindlabua - avatar
+ 4
buena hmno tmb soy d chile
7th Nov 2016, 1:36 AM
Rodrigo Tallar
Rodrigo Tallar - avatar
17th Nov 2017, 1:42 AM
#RahulVerma
#RahulVerma - avatar
+ 1
Your english is mucho bueno que mi espanol!
8th Nov 2016, 11:08 PM
Mark van Gennip
Mark van Gennip - avatar
+ 1
I'm not higher math literate(yet) but I managed to get Fibonacci series from Ruby=>Recursion(1/2) task my own way. I started doing a crazy downward algebraic pyramid spreadout first, but as I started running out of screen space I realised I could do some REFACTORING :D and here's a method of getting Fibonacci sequence from any integer. I used 10. Never encountered this problem mathematically before so bear with me. I'll call this an elevator trip method :D fib(10)= ?? go down | So, here we are back again! V fib(10)=fib(9)+fib(8) = 89 fib(9) = ?? go down | /\ V fib(9) = fib(8)+fib(7) = 55 fib(8) = ?? go down | /\ V fib(8) = fib(7)+fib(6) = 34 fib(7) = ?? go down | /\ V fib(7) = fib(6)+fib(5) = 21 fib(6) = ?? go down | /\ V fib(6) = fib(5)+fib(4) = 13 fib(5) = ?? go down | /\ V fib(5) = fib(4)+fib(3) = 8 fib(4) = ?? go down | /\ V fib(4) = fib(3)+fib(2) = 5 fib(3) = ?? go down | /\ V fib(3) = fib(2)+fib(1) = 3 fib(2) = ?? go down | /\ V fib(2) = fib(1)+fib(0) = 2 /\ fib(1) = fib(0) + fib(?) = 1(by base case if num <2 num=1 of the code algorithm) fib(0)= ???? =1(by base case if num <2 num=1 of the code algorithm) so what's fib(0) and fib(1) it depends on algorithm we consider: def fib (num) if num <2 num=1 else fib (num-1) +fib (num-2) end end fib(0)=1 fib(1)=1 - both equal 1 by base case: if num <2 num=1 if we consider the same algorithm as above but with different base case: if num <2 num=num we get 0, 1, 1,
5th May 2017, 11:19 AM
Gribanov Andrey
Gribanov Andrey - avatar
+ 1
After about an hour of staring at this and watching videos to no avail, and not being familiar with the mathematical sequence as I am now... I messed around with the code and came up with this: # 0 returns num, which is 0 # 1 returns num, which is 1 # 2 = (fib(1) = 1) + (fib(0) = 0) = 1 # 3 = (fib(2) = 1) + (fib(1) = 1) = 2 # 4 = (fib(3) = 2) + (fib(2) = 1) = 3 # 5 = (fib(4) = 3) + (fib(3) = 2) = 5 ## Add this code to the end of yours to see the results of a range of nums: (0..5).each do |x| puts fib(x) end
5th Mar 2019, 12:42 PM
Callum Evans
Callum Evans - avatar
0
That was perfect! It's kinda what I was thinking, but wasn't sure. In the end, it appears that the only values that have a "real value" in the function are 0 and 1, then the others are got from them for the recursion. Thanks! :D
6th Nov 2016, 9:11 PM
Matias Schiaffino
Matias Schiaffino - avatar
0
Jajajaja buena loco! Estamos en todos lados!
7th Nov 2016, 1:40 AM
Matias Schiaffino
Matias Schiaffino - avatar
0
I can see (read) that hahaha
8th Nov 2016, 11:28 PM
Matias Schiaffino
Matias Schiaffino - avatar
0
I found it hard cause I was making a mistake by adding up all previous numbers fib , instead of just last two for example fib(5) will be fib (4) + fib (3) only and not fib (4) + fib (3) + fib (2) + fib (1) + fib(0)
4th Feb 2017, 6:20 PM
Saif ul Islam
Saif ul Islam - avatar