Created
June 2, 2009 14:58
-
-
Save sarahhodne/122276 to your computer and use it in GitHub Desktop.
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
square:~/code/euler[master]% ruby problem1.rb --solution | |
Project Euler | |
Problem 1 | |
============= | |
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
Find the sum of all the multiples of 3 or 5 below 1000. | |
Solution (calculating): | |
266333 |
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
require "project_euler" | |
problem = Euler::Problem.new(1, <<DESCRIPTION | |
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
Find the sum of all the multiples of 3 or 5 below 1000. | |
DESCRIPTION | |
) | |
def sum_multiples_of(num, up_to=1000) | |
multiples = [] | |
i = 1 | |
multiple = 0 | |
until multiple >= up_to | |
multiple = num * i | |
multiples << multiple if multiple < up_to | |
i += 1 | |
end | |
#puts multiples.inspect | |
sum_of_multiples = multiples.inject { |sum, n| sum + n } | |
end | |
problem.solve { sum_multiples_of(3, 1000) + sum_multiples_of(5, 1000) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment