From 147ad062c6ddbfcbbd1256fe7f8e57ce30b36d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E6=B7=BC?= Date: Sun, 7 Mar 2021 19:41:59 +0800 Subject: [PATCH] feat: added fibonacci.rb --- dynamic_programming/fibonacci.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 dynamic_programming/fibonacci.rb diff --git a/dynamic_programming/fibonacci.rb b/dynamic_programming/fibonacci.rb new file mode 100644 index 0000000..950d11a --- /dev/null +++ b/dynamic_programming/fibonacci.rb @@ -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 \ No newline at end of file