Created
July 18, 2014 18:34
-
-
Save jmscholen/8f68f78daa5362ee9508 to your computer and use it in GitHub Desktop.
Project Euler Problem 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CODE | |
****************************** | |
------------------------------ | |
def value_returned(number) | |
if is_divisible_by?(number) | |
return number | |
else | |
return 0 | |
end | |
end | |
def is_divisible_by?(number) | |
if number > 15 || number == 15 | |
return true if number %15 == 0 | |
end | |
return true if number %5 == 0 | |
return true if number %3 == 0 | |
end | |
______________________________________________________________________________ | |
RSPEC TESTED ALONG WITH PROBLEM | |
****************************************************************************** | |
______________________________________________________________________________ | |
require_relative 'project_euler_p1.rb' | |
describe 'Problem1' do | |
context "knows number is divisible by" do | |
it '3' do | |
expect(is_divisible_by?(3)).to be_truthy | |
end | |
it '5' do | |
expect(is_divisible_by?(5)).to be_truthy | |
end | |
it '15' do | |
expect(is_divisible_by?(15)).to be_truthy | |
end | |
end | |
context "knows number is not divisible by" do | |
it '3' do | |
expect(is_divisible_by?(1)).to be_falsey | |
end | |
it '5' do | |
expect(is_divisible_by?(1)).to be_falsey | |
end | |
it '15' do | |
expect(is_divisible_by?(1)).to be_falsey | |
end | |
end | |
context "returns value if divisible true" do | |
it '3' do | |
expect(value_returned(9)).to be_truthy | |
end | |
it '5' do | |
expect(value_returned(10)).to be_truthy | |
end | |
it '15' do | |
expect(value_returned(15)).to be_truthy | |
end | |
end | |
context "returns nil of not divisible try" do | |
it '3' do | |
expect(value_returned(1)).to be_equal(0) | |
end | |
it '5' do | |
expect(value_returned(1)).to be_equal(0) | |
end | |
it '15' do | |
expect(value_returned(1)).to be_equal(0) | |
end | |
end | |
end | |
_________________________________________________ | |
IRB COMMANDS | |
************************************************* | |
_________________________________________________ | |
2.0.0-p451 :001 > require './project_euler_p1' | |
=> true | |
2.0.0-p451 :002 > array = Array.new | |
=> [] | |
2.0.0-p451 :003 > 1.upto(999){|number| array << value_returned(number) } | |
=> 1 | |
2.0.0-p451 :004 > array.inject{|sum,x| sum + x } | |
=> 233168 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment