From 34b316db74e00e4c2f8802b4163485beca340e3c Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 7 Mar 2021 12:27:36 -0800 Subject: [PATCH 1/4] Solve fibonacci with golden ratio formula --- maths/fibonacci.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 maths/fibonacci.rb diff --git a/maths/fibonacci.rb b/maths/fibonacci.rb new file mode 100644 index 0000000..19e72dd --- /dev/null +++ b/maths/fibonacci.rb @@ -0,0 +1,43 @@ +# The Fibonacci numbers, commonly denoted F(n) form a sequence, +# called the Fibonacci sequence, such that each number is the sum +# of the two preceding ones, starting from 0 and 1. That is, +# +# F(0) = 0, F(1) = 1 +# F(n) = F(n - 1) + F(n - 2), for n > 1. +# Given n, calculate F(n). + +# +# Approach: Math +# + +# Intuition: Using the golden ratio, a.k.a Binet's formula + +# Algorithm: Use the golden ratio formula to calculate the Nth Fibonacci number. +# https://demonstrations.wolfram.com/GeneralizedFibonacciSequenceAndTheGoldenRatio/ + +# Complexity Analysis + +# Time complexity: O(1). Constant time complexity since we are using no loops or recursion +# and the time is based on the result of performing the calculation using Binet's formula. +# Space complexity: O(1). The space used is the space needed to create the variable +# to store the golden ratio formula. + +def fib(n) + golden_ratio = (1 + 5**0.5) / 2 + ((golden_ratio**n + 1) / 5**0.5).to_i +end + +n = 2 +# Output: 1 +# Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. +puts(fib(n)) + +n = 3 +# Output: 2 +# Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. +puts(fib(n)) + +n = 4 +# Output: 3 +# Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. +puts(fib(n)) From 556228305f7ed907528e7edf2c46131281223b58 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 7 Mar 2021 20:27:49 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f46c03d..410aaed 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -38,6 +38,7 @@ * [Ceil](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil.rb) * [Ceil Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil_test.rb) * [Decimal To Binary](https://github.com/TheAlgorithms/Ruby/blob/master/maths/decimal_to_binary.rb) + * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/maths/fibonacci.rb) * [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb) * [Square Root](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root.rb) * [Square Root Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root_test.rb) From 630def0e26b098da0544f3331d0f7d01ec5d7543 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 7 Mar 2021 12:51:40 -0800 Subject: [PATCH 3/4] Update fibonacci.rb --- maths/fibonacci.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/fibonacci.rb b/maths/fibonacci.rb index 19e72dd..f2f77e4 100644 --- a/maths/fibonacci.rb +++ b/maths/fibonacci.rb @@ -4,6 +4,7 @@ # # F(0) = 0, F(1) = 1 # F(n) = F(n - 1) + F(n - 2), for n > 1. +# # Given n, calculate F(n). # @@ -22,7 +23,7 @@ # Space complexity: O(1). The space used is the space needed to create the variable # to store the golden ratio formula. -def fib(n) +def fibonacci(n) golden_ratio = (1 + 5**0.5) / 2 ((golden_ratio**n + 1) / 5**0.5).to_i end @@ -30,14 +31,14 @@ end n = 2 # Output: 1 # Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. -puts(fib(n)) +puts(fibonacci(n)) n = 3 # Output: 2 # Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. -puts(fib(n)) +puts(fibonacci(n)) n = 4 # Output: 3 # Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. -puts(fib(n)) +puts(fibonacci(n)) From f3e7b55492f4a72e2e7b7186f7a9fe3ad5a82470 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 7 Mar 2021 13:12:23 -0800 Subject: [PATCH 4/4] Update fibonacci.rb --- maths/fibonacci.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/fibonacci.rb b/maths/fibonacci.rb index f2f77e4..4515de2 100644 --- a/maths/fibonacci.rb +++ b/maths/fibonacci.rb @@ -20,6 +20,7 @@ # Time complexity: O(1). Constant time complexity since we are using no loops or recursion # and the time is based on the result of performing the calculation using Binet's formula. +# # Space complexity: O(1). The space used is the space needed to create the variable # to store the golden ratio formula. @@ -29,16 +30,16 @@ def fibonacci(n) end n = 2 +puts(fibonacci(n)) # Output: 1 # Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. -puts(fibonacci(n)) n = 3 +puts(fibonacci(n)) # Output: 2 # Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. -puts(fibonacci(n)) n = 4 +puts(fibonacci(n)) # Output: 3 # Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. -puts(fibonacci(n))