Module: Fibonacci

Included in:
Fixnum, Float
Defined in:
lib/closest_fibonacci.rb,
bin/closest_fibonacci

Instance Method Summary collapse

Instance Method Details

#closest_fibonacciObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/closest_fibonacci.rb', line 5

def closest_fibonacci
      
  def fibonaccit(n)
    return n if (0..1).include? n
    fibonaccit(n-1) + fibonaccit(n-2) if n > 1
  end
  
  i = 0
  
  while fibonaccit(i) < self do
    
    i = i + 1;
        
  end
  
  fibonaccit(i - 1)

end

#fibonaccit(n) ⇒ Object



7
8
9
10
# File 'lib/closest_fibonacci.rb', line 7

def fibonaccit(n)
  return n if (0..1).include? n
  fibonaccit(n-1) + fibonaccit(n-2) if n > 1
end