This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let toVarint v = | |
let rec loop n s = | |
match n >>> 7, n &&& 127 with | |
| (0, c) -> byte c :: s | |
| (r, c) -> loop r (byte (128 ||| c) :: s) | |
loop v [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r @"System.Reactive.dll" | |
open System | |
open System.Reactive.Subjects | |
open System.Reactive.Linq | |
let run n m = | |
let now = DateTime.Now | |
let nodes = List.init n (fun _ -> new Subject<_>()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
type Agent<'a> = MailboxProcessor<'a> | |
type Msg = Step of int | End | |
let getRingHead n = | |
let lastNode = | |
Agent.Start(fun inbox -> | |
let rec loop (time : DateTime) = async { | |
let! m = inbox.Receive() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let n = 1000;; | |
[1..n-1] | |
|> Seq.filter (fun v -> v % 3 = 0 || v % 5 = 0) | |
|> Seq.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Seq.unfold (fun (c, n) -> Some(c, (n, c + n))) (1, 2) | |
|> Seq.takeWhile (fun v -> v <= 4000000) | |
|> Seq.filter (fun v -> v % 2 = 0) | |
|> Seq.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let getLPF num = | |
let rec getLPF l rest = | |
seq { yield 2L; yield! seq {3L..2L..(rest |> float |> sqrt |> int64)}} | |
|> Seq.tryFind (fun d -> rest % d = 0L) | |
|> function | |
| None -> max l rest | |
| Some d -> getLPF (max l d) (rest / d) | |
getLPF 1L num;; | |
getLPF 600851475143L |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let generate a b = seq { | |
yield (a, b) | |
let rec down (a, b) diff secondPass = seq { | |
let next = a - diff, b + diff - 1 | |
yield next | |
yield! up next diff (not secondPass) | |
} | |
and up (a,b) diff secondPass = seq { | |
yield! Seq.init diff (fun i -> a + 1 + i, b - 1 - i) | |
yield! down (a + diff, b - diff) (if secondPass then diff + 1 else diff) secondPass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sumOfSquares n = | |
[1..n] | |
|> Seq.map (fun x -> x * x) | |
|> Seq.sum | |
let squareOfSum n = | |
let v = [1..n] |> Seq.sum | |
v * v | |
let diff n = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec primes = | |
let isPrime n = | |
primes | |
|> Seq.takeWhile (fun v -> v <= (float n |> sqrt |> int64)) | |
|> (not << Seq.exists (fun v -> n % v = 0L)) | |
let rec primes' n = seq { | |
if isPrime n then | |
yield n | |
yield! primes' (n + 2L) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let source = | |
let src = @"73167176531330624919225119674426574742355349194934 | |
96983520312774506326239578318016984801869478851843 | |
85861560789112949495459501737958331952853208805511 | |
12540698747158523863050715693290963295227443043557 | |
66896648950445244523161731856403098711121722383113 | |
62229893423380308135336276614282806444486645238749 | |
30358907296290491560440772390713810515859307960866 | |
70172427121883998797908792274921901699720888093776 | |
65727333001053367881220235421809751254540594752243 |
OlderNewer