Skip to content

Instantly share code, notes, and snippets.

@jgilless
Last active August 29, 2015 14:09
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 jgilless/405418cb87f41d84fd19 to your computer and use it in GitHub Desktop.
Save jgilless/405418cb87f41d84fd19 to your computer and use it in GitHub Desktop.
Ruby solution for Project Euler problem 1
# https://projecteuler.net/problem=1
puts "Find the sum of all the multiples of 3 or 5 below which number?"
STDOUT.flush #clears the buffer
max_num = Integer(gets.chomp)
def sum_x3_x5(num)
sum = 0
(1..num).each do |n|
sum = sum + n if (n % 3 == 0) || (n % 5 == 0) #Add if n is divisible by 3 or 5
end
sum #return
end
puts "Sum = " + sum_x3_x5(max_num).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment