Class: Fixnum
- Inherits:
-
Object
- Object
- Fixnum
- Defined in:
- lib/closest-fibonacci.rb
Overview
Yup, we’re adding functionality to Fixnum.
Instance Method Summary collapse
-
#closest_fibonacci ⇒ Object
Method to compute the next closest Fibonacci number F to a given integer N where F < N.
Instance Method Details
#closest_fibonacci ⇒ Object
Method to compute the next closest Fibonacci number F to a given integer N where F < N. For a Fixnumb, N = self’s value.
Fibonacci uses the O(log(n)) matrix method. Literate Programs Wiki!
Examples
143.closest_fibonacci # “Next Fibonacci number less than 143 is 89”
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/closest-fibonacci.rb', line 16 def closest_fibonacci i = 0 prior_fib = 0 this_fib = 0 begin prior_fib = this_fib this_fib = get_fibonacci(i) # puts "#{this_fib}\t\t#{this_fib - prior_fib}" i = i + 1 end until this_fib >= self prior_fib end |