Skip to content

Instantly share code, notes, and snippets.

@thenickcox
Created November 17, 2012 05:51
Show Gist options
  • Save thenickcox/4093645 to your computer and use it in GitHub Desktop.
Save thenickcox/4093645 to your computer and use it in GitHub Desktop.
Project Euler Problem 1
# 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