factorial


See On Github

Data

Contributor

Generic placeholder thumbnail

by mitogh

in ruby

Source Code

def factorial(n)
  (n <= 1) ? 1 : (n * factorial(n-1))
end

def factorial_iterative(n) 
  return 1 if n <= 1
  fib = 1
  while n >= 1 do 
      fib *= n
      n -= 1
  end
  fib
end
require './factorial'

describe "#factorial" do
  context "When given 0" do
    it "returns 1" do 
        expect(factorial(0)).to eq 1
    end
  end
  context "When given 5" do
    it "returns 120" do 
        expect(factorial(5)).to eq 120
    end
  end
end

describe "#factorial_iterative" do
  context "When given 0" do
    it "returns 1" do 
        expect(factorial_iterative(0)).to eq 1
    end
  end
  context "When given 5" do
    it "returns 120" do 
        expect(factorial_iterative(5)).to eq 120
    end
  end
end