Wednesday, May 09, 2007

Project Euler

Grant has been trying to bully me into writing some Haskell code. The straw that broke the camel's back was the challenge to solve "Project Euler: Problem 1", well really the problem was not that interesting, but we had a good spread of languages in the office everyone wanted to solve the problem in, so this is a fun opportunity to compare writing the solution in our own pet languages. Grant used Scheme, Geoff: Ruby, Kartik: C++, Dan: Delphi, Chris was out sick, but I bet he'll do it in C#. Me, I'm going to use Haskell.

We had already discussed earlier in the day that there is an equation to give you the sum, but in the spirit of all following the same approach, I won't be using it.

The plan is for everyone to post the implementation in their blog, so without further delay, here is mine:
sum [n | n <- [1..999], mod n 3 == 0 || mod n 5 == 0 ]]

2 comments:

Anonymous said...

Would the sum function take care of overlapping multiples? For instance both 3
and 5's multiples merge at 15, 30, 45 etc.

Anonymous said...

It wont add twice if thats what you are asking, since it adds if n mod 5 = 0 or n mod 3 = 0.