In order to learn F#, I decided to work through the Project Euler problem sets.   This is my code for solving problem one:

let answer n = [ for x in 1 .. n-1 when x % 3 = 0 or x % 5 = 0 -> x ]|> List.fold_left (+) 0
printfn "sum = %i" (answer 1000)
And I also did in with Linq:
Int32Range IntRange = new Int32Range(1, topRange - 1);
int answer = (from x in IntRange
              where (x % 3 == 0) || (x % 5 == 0)
              select x).Sum();
or
 
int answer = Enumerable.Range(1, topRange - 1).Where(x => (x % 3 == 0) || (x % 5 == 0)).Sum();
 
The Int32Range class comes from the book "C# in Depth" by Jon Skeet.

 

del.icio.us Tags: ,,