TheAlgorithms / Ruby

All algorithms implemented in Ruby
MIT License
1.19k stars 294 forks source link

Add more ways to calculate Fibonacci #144

Closed maxbarsukov closed 3 years ago

maxbarsukov commented 3 years ago

I would like to add a few ways to calculate the Fibonacci numbers, for example:

I have a couple of ways to do this, and so I would like some advice.

1. So, the first way is to use the strategy pattern, for example

  fibonacci = FibonacciContext.new(FibonacciRecursive.new)
  puts fibonacci.compute 10

  fibonacci.strategy = FibonacciMatrix.new
  puts fibonacci.compute 10

with file structure like:

  (math)
  ├── fibonacci
  │      ├── strategies
  │      │      ├── fibonacci_recursive.rb
  │      │      ├── fibonacci_matrix.rb
  │      │      └── fibonacci_golden_ratio.rb
  │      ├── strategy.rb
  │      └── fibonacci_context.rb
  └── fibonacci.rb <--- client file

2. The second way is to make a Fibonacci module with different ways as functions and divide it into several files with this structure:

  (math)
  ├── fibonacci
  │   ├── recursive.rb
  │   ├── matrix.rb
  │   └── golden_ratio.rb
  └── fibonacci.rb

where

  # fibonacci.rb
  require_relative 'fibonacci/recursive.rb'
  require_relative 'fibonacci/matrix.rb'
  require_relative 'fibonacci/golden_ratio.rb'

  module Fibonacci
  end

  # fibonacci/recursive.rb
  module Fibonacci
    def recursive(n)
      # ...
    end
  end

  ...
  1. The third and simplest is to make just a lot of files in the math folder:
    (math)
    ├── fibonacci_recursive.rb
    ├── fibonacci_matrix.rb
    └── fibonacci_golden_ratio.rb

Well, I have given several ways, but I myself tend to the third, since most of the algorithms are designed in this way. But, on the other hand, it could be a starting point for refactoring the project structure.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

Please reopen this issue once you add more information and updates here. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions!