mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-28 19:58:20 +01:00
feat: added fibonacci.rb
This commit is contained in:
parent
2e986753cd
commit
147ad062c6
1 changed files with 8 additions and 0 deletions
8
dynamic_programming/fibonacci.rb
Normal file
8
dynamic_programming/fibonacci.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Given a number, calculates the fibonacci number.
|
||||
|
||||
def fibonacci(number, memo_hash = {})
|
||||
if number == 0 || number == 1
|
||||
return number
|
||||
end
|
||||
memo_hash[number] ||= fibonacci(number - 1, memo_hash) + fibonacci(number -2, memo_hash)
|
||||
end
|
Loading…
Add table
Reference in a new issue