Created
November 17, 2012 05:51
-
-
Save thenickcox/4093645 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
# solves http://projecteuler.net/problem=1 | |
# not the shortest solution, but one with a more versatile and reusable method | |
def divisibles(up_to, divisible_by_a, divisible_by_b) | |
rangeArray = (1..(up_to -1)).to_a | |
just_multiples = [] | |
rangeArray.each do |x| | |
if ( x % divisible_by_a == 0 || x % divisible_by_b == 0 ) | |
just_multiples << x | |
end | |
end | |
just_multiples.inject { |x, sum| sum + x } | |
end | |
divisibles(1000, 3, 5) | |
# => 233168 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment