Skip to content

Instantly share code, notes, and snippets.

@dungpa
Created February 10, 2012 16:29
Show Gist options
  • Save dungpa/1790646 to your computer and use it in GitHub Desktop.
Save dungpa/1790646 to your computer and use it in GitHub Desktop.
Count all solutions sequentially and in parallel
// 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.
let countParallel2 size =
let n = (size-1)*(size-2)
let boards = Array.init n (fun _ -> new Board(size))
let mutable count = 0
for i in 0..size-1 do
for j in 0..size-1 do
if abs(i-j) > 1 then // safe to place two queens together
boards.[count].placeQueen(i)
boards.[count].placeQueen(j)
count <- count+1
boards |> Array.Parallel.map (fun b -> b.countSolutions())
|> Array.sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment