Skip to content

Commit fc37dda

Browse files
committed
Demonstrating dotnet/fsharp#7422
1 parent 801ac4c commit fc37dda

File tree

2 files changed

+21
-44
lines changed

2 files changed

+21
-44
lines changed

src/Cistern.Linq.FSharp/Linq.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ open System
77
open System.Runtime.CompilerServices
88

99
type Linq =
10-
[<MethodImpl(MethodImplOptions.NoInlining)>]
10+
// Uncomment this to make this code work
11+
//[<MethodImpl(MethodImplOptions.NoInlining)>]
1112
static member unfold (f:'State->option<'T*'State>) (seed:'State) : seq<'T> = Consumables.Unfold (f, seed, Links.Identity.Instance) :> seq<'T>
1213

1314
static member inline map (f:'a->'b) (e:seq<'a>) = Enumerable.Select (e, f)
Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,46 @@
11
// Learn more about F# at http://fsharp.org
22

3-
open System
43
open Cistern.Linq.FSharp
54
open System.Diagnostics
65

76
[<EntryPoint>]
8-
let main argv =
7+
let main _ =
98
Register.RegisterFSharpCollections ()
109

11-
let listSize = 10000
12-
let iterations = 1
13-
let repetitions = 5
14-
15-
let x = Random 42
16-
let test = List.init listSize (fun n -> x.NextDouble ())
10+
let iterations = 1000000
11+
let repetitions = 3
1712

1813
for _i = 1 to repetitions do
1914
let sw = Stopwatch.StartNew ()
2015

21-
let mutable total = 0.
2216
for i = 1 to iterations do
23-
let triangleNumber(n:int64) = [1L..n] |> Seq.sum
24-
25-
let findFactorsOf(n:int64) =
26-
let upperBound = int64(Math.Sqrt(double(n)))
27-
[1L..upperBound]
28-
|> Seq.filter (fun x -> n % x = 0L)
29-
|> Seq.collect (fun x -> [x; n/x])
30-
31-
let naturalNumbers = Seq.unfold (fun x -> Some(x, x+1L)) 1L
17+
let fibonacciLinq = Linq.unfold (fun (current, next) -> Some(current, (next, current + next))) (0, 1)
3218

33-
let answer =
34-
naturalNumbers
35-
|> Seq.map (fun x -> triangleNumber(x))
36-
|> Seq.filter (fun x -> Seq.length(findFactorsOf(x)) >= 500)
37-
|> Seq.head
19+
let fibTotal =
20+
fibonacciLinq
21+
|> Linq.takeWhile (fun n -> n < 4000000)
22+
|> Linq.filter (fun n -> n % 2 = 0)
23+
|> Linq.sum
3824

39-
total <- total + float answer
25+
burning_monk_euler.Helpers.validate 4613732 fibTotal
4026

41-
printfn "Seq %d (%f)" sw.ElapsedMilliseconds total
27+
printfn "Cistern %dms" sw.ElapsedMilliseconds
4228

4329
for _i = 1 to repetitions do
4430
let sw = Stopwatch.StartNew ()
4531

4632
let mutable total = 0.
4733
for i = 1 to iterations do
48-
let triangleNumber(n:int64) = [1L..n] |> Linq.sum
34+
let fibonacciSeq = Seq.unfold (fun (current, next) -> Some(current, (next, current + next))) (0, 1)
4935

50-
let findFactorsOf(n:int64) =
51-
let upperBound = int64(Math.Sqrt(double(n)))
52-
[1L..upperBound]
53-
|> Linq.filter (fun x -> n % x = 0L)
54-
|> Linq.collect (fun x -> [x; n/x])
55-
56-
let naturalNumbers = Linq.unfold (fun x -> Some(x, x+1L)) 1L
57-
58-
let answer =
59-
naturalNumbers
60-
|> Linq.map (fun x -> triangleNumber(x))
61-
|> Linq.filter (fun x -> Linq.length(findFactorsOf(x)) >= 500)
62-
|> Linq.head
63-
64-
total <- total + float answer
65-
66-
printfn "Cistern %d (%f)" sw.ElapsedMilliseconds total
36+
let fibTotal =
37+
fibonacciSeq
38+
|> Seq.takeWhile (fun n -> n < 4000000)
39+
|> Seq.filter (fun n -> n % 2 = 0)
40+
|> Seq.sum
6741

42+
burning_monk_euler.Helpers.validate 4613732 fibTotal
6843

44+
printfn "Seq %dms" sw.ElapsedMilliseconds
6945

7046
0 // return an integer exit code

0 commit comments

Comments
 (0)