Created
December 28, 2012 22:32
-
-
Save fornext1119/4402596 to your computer and use it in GitHub Desktop.
Project Euler Problem 1 Ruby Version
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
#単純に加算 | |
sum = 0 | |
(1..999).each do |i| | |
if (i % 3) == 0 | |
sum += i | |
elsif (i % 5) == 0 | |
sum += i | |
end | |
end | |
puts sum | |
#等差数列の和 | |
sum = 0; | |
#初項, 公差 | |
[3, 5, 15].each do |a| | |
n = 999 / a #項数 | |
l = n * a; #末項 a+(n-1)d | |
case a | |
when 3, 5 | |
sum += ((a + l) * n / 2) | |
else | |
sum -= ((a + l) * n / 2) | |
end | |
end | |
puts sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment