Class: Fixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/fib.rb

Instance Method Summary collapse

Instance Method Details

#closest_fibonacciObject

return closest fibonacci number which is <= self E.g. 256.closest_fibonacci => 233



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fib.rb', line 4

def closest_fibonacci
  if self < 0
    return nil
  elsif self <= 3
    return self
  end

  fib_n_2,fib_n_1,fib = 1,2,3
  while self >= fib
    fib = fib_n_1 + fib_n_2
    fib_n_2, fib_n_1 = fib_n_1, fib
  end
  fib_n_2
end