Skip to content

Instantly share code, notes, and snippets.

@jessieay
Created June 14, 2012 05:24
Show Gist options
  • Save jessieay/2928155 to your computer and use it in GitHub Desktop.
Save jessieay/2928155 to your computer and use it in GitHub Desktop.
Fibonacci super efficiently
class Integer
def fibonacci(arr=[0,1])
arr[self] ||= (self-1).fibonacci(arr) + (self-2).fibonacci(arr)
# if arr[self].nil?
# arr[self] = (self-1).fibonacci(arr) + (self-2).fibonacci(arr)
# end
# arr[self]
#arr[self] ||= (self-1).fibonacci(arr) + (self-2).fibonacci(arr)
# return 0 if self < 1
#
# arr[self-1] ||= (self-1).fibonacci(arr)
# arr[self-2] ||= (self-2).fibonacci(arr)
#
# self < 3 ? 1 : arr[self-1] + arr[self-2]
end
end
# class Integer
# # @@fibs = {}
#
# def fibonacci
# return 0 if self < 1
# self < 3 ? 1 : (self-1).fibonacci + (self-2).fibonacci
# end
# end
start_time = Time.now
puts 10000.fibonacci
stop_time = Time.now
puts (stop_time-start_time) * 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment