Tuesday, June 15, 2010

Euler Problem 1

Problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

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


Solution:

function euler1 (range)
local result = 0

for i=3,range do
if (i % 3) == 0 or (i % 5) == 0 then
result = result + i
end
end

return result
end

print(euler1(999))


There's really nothing clever here, I just used the most straightforward way of accumulating the results imperatively. Mostly boring stuff, but now we are underway!

Monday, June 14, 2010

Project Euler

I am taking on a new endeavor:

Project Euler [http://projecteuler.net]

I am going to post solutions to these problems in a new language I'm learning, called Lua. [http://www.lua.org/]

This should help me exercise my programming skills while also helping me become more familiar with a new language. Here's why I picked Lua:

1) Powerful - Lua can be embedded in virtually any other environment and has mechanisms for doing just about anything you can think of.

2) Performant - Using LuaJit, it may be the fastest scripting / dynamic language out there.

3) Portable - It runs nearly everywhere without changing the code one bit. This is becoming more an interesting feature as the mobile world booms, with ARM processors.

4) Practical - It's a small language, with many similarities to JavaScript, but without a lot of the bad design in JavaScript. It's simple to use and just get things done. If it's not there in Lua, it's trivial to call a C function that does the job for you.

I look forward to posting my solutions, it should be fun and helpful. I hope everyone that stumbles on my blog by accident finds it as fun and interesting as I do!

-DR