Created
April 2, 2016 18:55
-
-
Save pdxwolfy/88637e590d540aa90d90efd49a7dfd49 to your computer and use it in GitHub Desktop.
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
#!/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