Skip to content

Instantly share code, notes, and snippets.

@pdxwolfy
Created April 2, 2016 18:55
Show Gist options
  • Save pdxwolfy/88637e590d540aa90d90efd49a7dfd49 to your computer and use it in GitHub Desktop.
Save pdxwolfy/88637e590d540aa90d90efd49a7dfd49 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
# Copyright (c) 2016 Pete Hanson
# frozen_string_literal: true
class Fibonacci
def self.nth n_th
raise ArgumentError, 'n_th must be >= 1' unless n_th >= 1
@fib = [1, 1]
next_to_last = @fib.first
while @fib.size <= n_th
last = @fib.last
@fib.push next_to_last + last
next_to_last = last
end
@fib[n_th - 1]
end
end
1.upto(10) { |number| puts Fibonacci.nth(number) }
puts Fibonacci.nth(10000)
puts Fibonacci.nth(100000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment