Friday, January 23, 2009

Project Euler - Problem 1

I found a great site called Project Euler. The site explains it best:

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context."

I've been using Ruby as an alternative to PowerShell, since it runs on the miriad of systems that I am exposed to in my current position. I figured that this would be a good way to increase my Ruby coding skills and have a little fun solving some puzzles at the same time. From time to time, I'll post my solution to a problem. I'm still learning, so if you have suggestions, I would love to hear your constructive criticism.

Problem 1:
Find the sum of all the multiples of 3 or 5 below 1000.

Solution:

ans=0

(1...1000).each{ |i|
  if( i%3 == 0 || i%5 == 0 ) then
    ans += i
  end
}

puts ans


I doesn't seem very "Ruby-ish" but it works.

No comments: