Archive for March 2009

Project Euler, very clean, useful and fun

Yesterday I descovered one of the best websites out there, it's http://projecteuler.net.

As noted in the introduction of the project here :
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.

It's about programming and mathematical problems, that you solve in your favorite language and submit them to the website to ckeck if your answer is correct and earn scores.

For me I decided to solve these problems in OCAML and C# using Linq, and guess what! My first answer using OCAML was right ;).

the first problem is the following :

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.

and this is my implementation in OCAML:

let gen_mult num max=

let rec gen i=

if i*num>=max then []

else (i*num)::gen (i+1)

in gen 1;;

let m3=gen_mult 3 1000;;

let m5=gen_mult 5 1000;;

let s3=List.fold_left (fun acc x->x+acc) 0 m3;;

let s5=List.fold_left (fun acc x->if x mod 3=0 then acc else x+acc) 0 m5;;

let result=s3+s5;;

have fun :)

Posted in , , |

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.