Skip to content

Instantly share code, notes, and snippets.

@fornext1119
Created December 28, 2012 22:32
Show Gist options
  • Save fornext1119/4402596 to your computer and use it in GitHub Desktop.
Save fornext1119/4402596 to your computer and use it in GitHub Desktop.
Project Euler Problem 1 Ruby Version
#単純に加算
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