Created
July 17, 2013 15:19
-
-
Save elreimundo/6021559 to your computer and use it in GitHub Desktop.
Project Euler problem #1 : "Find the sum of all of the whole numbers less than 1000 that are multiples of either 3 or 5." I thought that problem was too quick, so I generalized it to "Find the sum of all of the whole numbers less than n that are multiples of either x or z," then ran n=1000,x=3,z=5.
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
def countofmultiples (num,firstfactor,secondfactor) | |
count = 0 | |
1.upto(num-1) do |check| | |
if check % firstfactor == 0 || check % secondfactor == 0 | |
count += check | |
end | |
end | |
count | |
end | |
puts countofmultiples(10,3,5) | |
#=>23 | |
puts countofmultiples(1000,3,5) | |
#=>233168 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment