Last active
January 3, 2016 06:29
-
-
Save phil-monroe/8422653 to your computer and use it in GitHub Desktop.
Fibonacci Sequence Generator with Dynamic Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
def fib(n) | |
raise ArgumentError, "needs to be positive! n: #{n}" if n < 0 | |
Array.new(n, 1).tap do |seq| | |
(2..seq.length-1).each do |idx| | |
seq[idx] = seq[idx-1] + seq[idx-2] | |
end if n > 2 | |
end | |
end | |
(0..10).each do |i| | |
puts fib(i).inspect | |
end | |
begin | |
fib(-1) | |
rescue => e | |
puts e.inspect | |
end | |
# # puts fib(100000).inject(&:+) | |
puts Benchmark.realtime{ fib(200000).inject(&:+) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment