Last active
August 29, 2015 14:09
-
-
Save jgilless/405418cb87f41d84fd19 to your computer and use it in GitHub Desktop.
Ruby solution for Project Euler problem 1
This file contains 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
# 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