module FactorialFlight

Constants

VERSION

Public Instance Methods

factorial(method = :inject) click to toggle source
# File lib/factorial_flight.rb, line 4
def factorial(method = :inject)
  return 'Can not calculate factorial of a negative number' if self < 0

  case method
  when :gamma
    gamma_method
  when :loop
    loop_method
  when :recursive
    recursive_method
  else
    inject_method
  end
end

Private Instance Methods

gamma_method() click to toggle source
# File lib/factorial_flight.rb, line 20
def gamma_method
  Math::gamma(self + 1).to_i
end
inject_method() click to toggle source
# File lib/factorial_flight.rb, line 24
def inject_method
  return 1 if self < 1
  (1..self).inject(:*)
end
loop_method() click to toggle source
# File lib/factorial_flight.rb, line 29
def loop_method
  current_count = 1

  (1..self).each do |num|
    current_count *= num
  end

  current_count
end
recursive_method() click to toggle source
# File lib/factorial_flight.rb, line 39
def recursive_method
  if self <= 1
    1
  else
    self * (self - 1).factorial
  end
end