Skip to content

Instantly share code, notes, and snippets.

@elreimundo
Created July 17, 2013 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elreimundo/6021559 to your computer and use it in GitHub Desktop.
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.
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