**New course!**Every coder should learn

**Generative AI!**

+ 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

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

+ 4

buena hmno tmb soy d chile

+ 1

Your english is mucho bueno que mi espanol!

+ 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,

+ 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

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

0

Jajajaja buena loco! Estamos en todos lados!

0

I can see (read) that hahaha

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)