Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Created December 4, 2013 01:31
Show Gist options
  • Save bjhaid/7780828 to your computer and use it in GitHub Desktop.
Save bjhaid/7780828 to your computer and use it in GitHub Desktop.
require 'rspec'
describe "Fib" do
it "should return 0" do
expect(fib 0).to eq 0
end
it "should return 1" do
expect(fib 1).to eq 1
end
it "should return 1" do
expect(fib 2).to eq 1
end
it "should return 2" do
expect(fib 3).to eq 2
end
it "should return 3" do
expect(fib 4).to eq 3
end
it "should return 5" do
expect(fib 5).to eq 5
end
it "should return 8" do
expect(fib 6).to eq 8
end
it "should return 89" do
expect(fib 11).to eq 89
end
it "should return 6765" do
expect(fib 20).to eq 6765
end
end
def fib n
i, fibs = 0, []
while i <= n
if i == 0
fibs.push 0
elsif i == 1 || i == 2
fibs.push 1
else
fibs.push(fibs.last + fibs[-2])
end
i += 1
end
fibs.last
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment