Created
April 1, 2016 00:15
-
-
Save ekingery/83e0987eb0b640a3ecee5e5ee32c7bf5 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
# project euler problem 1 | |
require 'minitest/autorun' | |
class Problem1 | |
def x_multiple_of_y?(x, y) | |
x % y == 0 ? true : false | |
end | |
def sum_multiples_below(capnum) | |
i = 1 | |
sum = 0 | |
while i < capnum do | |
if x_multiple_of_y?(i,3) | |
sum += i | |
elsif x_multiple_of_y?(i,5) | |
sum += i | |
end | |
i += 1 | |
end | |
return sum | |
end | |
end | |
# test problem1 solution | |
class TestProblem1 < Minitest::Test | |
def setup | |
@problem1 = Problem1.new | |
end | |
def test_factors | |
assert_equal false, @problem1.x_multiple_of_y?(1, 3) | |
assert_equal false, @problem1.x_multiple_of_y?(1, 5) | |
assert_equal true, @problem1.x_multiple_of_y?(3, 3) | |
assert_equal false, @problem1.x_multiple_of_y?(3, 5) | |
assert_equal false, @problem1.x_multiple_of_y?(5, 3) | |
assert_equal true, @problem1.x_multiple_of_y?(5, 5) | |
assert_equal true, @problem1.x_multiple_of_y?(15, 3) | |
assert_equal true, @problem1.x_multiple_of_y?(15, 5) | |
assert_equal false, @problem1.x_multiple_of_y?(7, 3) | |
assert_equal false, @problem1.x_multiple_of_y?(7, 5) | |
end | |
def test_answers | |
assert_equal 23, @problem1.sum_multiples_below(10) | |
end | |
def test_solveit | |
puts @problem1.sum_multiples_below(1000) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment