Method: Math#fibs

Defined in:
lib/magician/math.rb

#fibs(length) ⇒ Array

Calculates a series of Fibonacci numbers of a specified length. Returns nil if a negative length is given.

returned

(ordered)

Parameters:

  • length (Integer)

    the length of the Fibonacci series that should be

Returns:

  • (Array)

    a Fibonacci series of Integers with the specified length



111
112
113
114
115
116
117
118
119
# File 'lib/magician/math.rb', line 111

def fibs length
  return nil if length < 0
  terms = []
  until terms.length == length do
    at_beginning = [0,1].include? terms.length
    terms << ( at_beginning ? 1 : terms[-2] + terms[-1] )
  end
  terms
end