Skip to content

Instantly share code, notes, and snippets.

@sowasred2012
Created April 16, 2014 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sowasred2012/10850981 to your computer and use it in GitHub Desktop.
Save sowasred2012/10850981 to your computer and use it in GitHub Desktop.
Made Code Dojo 3: Even Fibonacci Number
FAILED
http://projecteuler.net/problem=2
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
require 'rspec'
require 'fibonacci.rb'
describe FibonacciCalculator do
before(:each) do
described_class.new
end
describe ".sequence" do
it "should return all numbers in the fibonacci sequence below twenty one" do
described_class.sequence(21).should eq([0,1,1,2,3,5,8,13])
end
it "should raise an error if a negative number is passed in" do
expect { @calc.sequence(-1) }.to raise_error InvalidArgumentException
end
it "should raise an error if a negative number is passed in" do
expect { @calc.sequence("a") }.to raise_error InvalidArgumentException
end
it "should return all numbers in the fibonacci sequence below ninety" do
described_class.new.sequence(90).should eq([0,1,1,2,3,5,8,13,21,34,55,89])
end
it "should return all numbers in the fibonacci sequence below eighty nine" do
described_class.new.sequence(89).should eq([0,1,1,2,3,5,8,13,21,34,55])
end
end
describe ".sequence_even" do
it "should return all even numbers in the sequence" do
@calc.sequence_even(90).should eq([0,2,8,34])
end
end
describe ".product" do
it "should return the product of all even numbers" do
@calc.product_even(90).should eq(44)
@calc.product_even(21).should eq(10)
end
end
end
require 'rspec'
require 'fibonacci.rb'
describe FibonacciCalculator do
before(:each) do
described_class.new
end
describe ".sequence" do
it "should return all numbers in the fibonacci sequence below twenty one" do
described_class.new.new(21)sequence(21).should eq([0,1,1,2,3,5,8,13])
end
it "should raise an error if a negative number is passed in" do
expect { @calc.sequence(-1) }.to raise_error InvalidArgumentException
end
it "should raise an error if a negative number is passed in" do
expect { @calc.sequence("a") }.to raise_error InvalidArgumentException
end
it "should return all numbers in the fibonacci sequence below ninety" do
described_class.new.sequence(90).should eq([0,1,1,2,3,5,8,13,21,34,55,89])
end
it "should return all numbers in the fibonacci sequence below eighty nine" do
described_class.new.sequence(89).should eq([0,1,1,2,3,5,8,13,21,34,55])
end
end
describe ".sequence_even" do
it "should return all even numbers in the sequence" do
@calc.sequence_even(90).should eq([0,2,8,34])
end
end
describe ".product" do
it "should return the product of all even numbers" do
@calc.product_even(90).should eq(44)
@calc.product_even(21).should eq(10)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment