## Project Euler #1

F# has a lot of functionality for manipulation of lists. Here is my first attempt at the solution in F#.

let numbers35 = List.filter (fun i -> i % 3 = 0 or i % 5 = 0) [1 .. 999];;
printfn "Euler project problem 1 : %d" (List.fold_left (+) 0 numbers35);;


I could have merged the two lines into one. However, I find this a bit more clear. The filter function filters (!) based on the boolean function given as the first argument. The fold_left function adds all numbers that made it through the filter.

Another option is to use the mathematical formula for the sum

$$\sum_{k=1}^n k = \frac{1}{2} n (n+1)$$

A little thought tells us that we can compute the result using the above sum three times. One for the elements divisible with 3, one for the ones divisible by 5 and finally one divisible by 15 in order to remove those elements divisible by both 3 and 5 (which, otherwise, would be added twice). That is,

$$3\sum_{k=1}^{333}k +5\sum_{k=1}^{199}k -15\sum_{k=1}^{66}k = \frac{3 \cdot 333 \cdot 334 + 5 \cdot 199 \cdot 200 - 15 \cdot 66 \cdot 67 }{2}$$

You can use the comment system below or send my an email mic.jacobsen@gmail.com