This file contains hidden or 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
| type Board(initialSize: int) = | |
| let size = initialSize | |
| let mutable col = 0 | |
| let rows = Array.create size false | |
| let fwDiagonals = Array.create (2*size-1) false | |
| let bwDiagonals = Array.create (2*size-1) false |
This file contains hidden or 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
| member x.safe(row) = | |
| not (rows.[row] || fwDiagonals.[row + col] | |
| || bwDiagonals.[row - col + size - 1]) | |
| member x.placeQueen(row) = | |
| rows.[row] <- true | |
| fwDiagonals.[row + col] <- true | |
| bwDiagonals.[row - col + size - 1] <- true | |
| col <- col+1 | |
This file contains hidden or 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
| member x.safe(row) = | |
| not (rows.[row] || fwDiagonals.[row + col] | |
| || bwDiagonals.[row - col + size - 1]) | |
| member x.placeQueen(row) = | |
| rows.[row] <- true | |
| fwDiagonals.[row + col] <- true | |
| bwDiagonals.[row - col + size - 1] <- true | |
| col <- col+1 | |
This file contains hidden or 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
| // Place the first queen beforehand for parallel execution. | |
| let countParallel1 size = | |
| let boards = Array.init size (fun _ -> new Board(size)) | |
| for i in 0..size-1 do | |
| boards.[i].placeQueen(i) | |
| boards |> Array.Parallel.map (fun b -> b.countSolutions()) | |
| |> Array.sum | |
| // Place two first queens beforehand for parallel execution. |
This file contains hidden or 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 lucasLehmer p = | |
| let m = (1I <<< p) - 1I | |
| let rec loop i acc = | |
| if i = p-2 then acc | |
| else loop (i+1) ((acc*acc - 2I)%m) | |
| (loop 0 4I) = 0I |
This file contains hidden or 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 inline mersenne (i: int) = | |
| if i = 2 || (isPrime (bigint i) && lucasLehmer i) then | |
| let p = 1I <<< i | |
| Some (i, (p/2I) * (p-1I)) | |
| else None | |
| let runPerfectsSeq n = | |
| seq {1..n} | |
| |> Seq.choose mersenne | |
| |> Seq.toArray |
This file contains hidden or 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 primesUnder = function | |
| | n when n<=2 -> [||] | |
| | 3 -> [|2|] | |
| | n -> let ns = n |> float |> sqrt |> ceil |> int | |
| let smallers = primesUnder ns | |
| Array.append smallers (filterRange (indivisible smallers) (ns, n-1)) |
This file contains hidden or 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 filterRange predicate (i, j) = | |
| let results = ResizeArray(j-i+1) // reserve quite a lot of space. | |
| for k = i to j do | |
| if predicate k then results.Add(k) | |
| results.ToArray() | |
| let indivisible divisors b = | |
| Array.forall (fun a -> b%a<>0) divisors |
This file contains hidden or 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 pfilterRange predicate (i, j) = | |
| let results = ResizeArray(j-i+1) | |
| let monitor = new Object() | |
| Parallel.For( | |
| i, j, new ParallelOptions(), | |
| (fun () -> ResizeArray(j-i+1)), | |
| (fun k _ (localList: ResizeArray<_>) -> | |
| if predicate k then localList.Add(k) | |
| localList), | |
| (fun local -> lock (monitor) (fun () -> results.AddRange(local))) |
This file contains hidden or 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 languages = Domain.Enum("English", "Spanish", | |
| "Japanese", "Italian", "Norwegian") | |
| let language = Decision(languages, "Language") | |
| person.AddDecision(language) |
OlderNewer