Module: Proxinacci

Defined in:
lib/proxinacci.rb

Instance Method Summary collapse

Instance Method Details

#closest_fibonacciObject



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

def closest_fibonacci
  # Don't allow any zero or negative values
  return nil if self <= 0
  
  x, y = 0, 1
  while y < self
    # Progress through the Fibonacci sequence while y is less
    # than the number calling the method
    x, y = y, x + y
  end
  
  # At this point x is the largest Fibonacci number smaller than the
  # number calling the method
  return x
end