Class: Fixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/fibonacho.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/fibonacho.rb', line 2

def closest_fibonacci
  # Basic idea:
  # Generate a fibinacci sequence in a loop. At each iteration check to see if the last number created is greater than or equal to the input.
  # If it is, break and return that last number. Otherwise keep the loop running
  
  # Here a and b are standins for the current pair of numbers in the sequence. According to Fn = Fn-1 + Fn-2 the next number is the sum of the previous two numbers (a + b).
  # While we're determining the next number, we assign b to a so that the current highest number in the seqence isn't lost
  a = 0
  b = 1
  while b < self
    a,b = b,a+b
  end
  
  return a    
end