lib/compsci/fibonacci.rb in compsci-0.1.1.1 vs lib/compsci/fibonacci.rb in compsci-0.2.0.1
- old
+ new
@@ -1,9 +1,9 @@
autoload :Matrix, 'matrix'
module CompSci
- class Fibonacci
+ module Fibonacci
def self.classic(n)
n < 2 ? n : classic(n-1) + classic(n-2)
end
def self.cache_recursive(n, cache = {})
@@ -15,21 +15,13 @@
cache = [0, 1]
2.upto(n) { |i| cache[i] = cache[i-1] + cache[i-2] }
cache[n]
end
- # traditional
def self.dynamic(n)
a, b = 0, 1
n.times { a, b = b, a+b }
a
- end
-
- # fails for n == 0
- def self.dynamic_fast(n)
- a, b = 0, 1
- (n-1).times { a, b = b, a+b }
- b
end
# https://gist.github.com/havenwood/02cf291b809327d96a3f
# slower than dynamic until around n == 500
def self.matrix(n)