4 people like it.
Like the snippet!
Project Euler #1
This snippet is code that solves first Project Euler problem. It finds the sum of all the multiples of 3 or 5 below 1000. Please add other (more efficient, succinct or interesting) solutions to this snippet.
1:
2:
3:
4:
5:
6:
|
let range = [0..999]
let condition x = x % 3 = 0 || x % 5 = 0
range
|> List.filter condition
|> List.fold (+) 0
|
1:
2:
3:
|
[0..999]
|> Seq.filter (fun x -> x % 3 = 0 || x % 5 = 0)
|> Seq.reduce (+)
|
1:
2:
3:
|
seq { for x in 0 .. 999 do
if x % 3 = 0 || x % 5 = 0 then yield x }
|> Seq.sum
|
1:
2:
3:
4:
5:
6:
|
let rec multiple x =
if x = 1000 then 0
elif x%3 = 0 || x%5 = 0 then x + multiple (x+1)
else multiple (x+1)
multiple 1
|
1:
2:
3:
4:
5:
6:
7:
8:
9:
|
let rec mul' acc x =
match x with
| 999L -> acc + x
| x when (x%3L=0L||x%5L=0L) ->
mul' (acc + x) (x + 1L)
| _ ->
mul' acc (x + 1L)
mul' 0L 1L
|
val range : int list
Full name: Script.range
val condition : x:int -> bool
Full name: Script.condition
val x : int
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val filter : predicate:('T -> bool) -> list:'T list -> 'T list
Full name: Microsoft.FSharp.Collections.List.filter
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State
Full name: Microsoft.FSharp.Collections.List.fold
module Seq
from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.filter
val reduce : reduction:('T -> 'T -> 'T) -> source:seq<'T> -> 'T
Full name: Microsoft.FSharp.Collections.Seq.reduce
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.Seq.sum
val multiple : x:int -> int
Full name: Script.multiple
val mul' : acc:int64 -> x:int64 -> int64
Full name: Script.mul'
val acc : int64
val x : int64
More information