Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique: Difference between revisions
Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique (view source)
Revision as of 16:12, 20 August 2019
, 4 years ago→{{header|F_Sharp|F#}}: i' j' and z' are idiotmatic F# but Rosettacode's syntax highlighting doesn't cope
(→{{header|Go}}: First version now far quicker thanks to advice by Nigel Galloway and second version removed as no longer necessary.) |
(→{{header|F_Sharp|F#}}: i' j' and z' are idiotmatic F# but Rosettacode's syntax highlighting doesn't cope) |
||
Line 19:
<lang fsharp>
// Jacobson and Matthews technique for generating Latin Squares. Nigel Galloway: August 5th., 2019
let jmLS α X0=
let X0=Array2D.copy X0
let N=let N=[|[0..α-1];[α-1..(-1)..0]|] in (fun()->N.[R 2])
let rec randLS i j z
X0.[i,
▲ |false->randLS i' j' z' (List.find(fun n->X0.[n,j']=z')(N())) (List.find(fun n->X0.[i',n]=z')(N())) (if (R 2)=0 then let t=X0.[i',j'] in X0.[i',j']<-z; t else z)
let i,j=R α,R α
let z =let z=1+(R (α-1)) in if z<X0.[i,j] then z else 1+(z+1)%α
let
X0.[i,j]<-z; randLS i j z
let asNormLS α=
Line 69 ⟶ 67:
;part 2
<lang fsharp>
randLS
</lang>
{{out}}
Line 124 ⟶ 122:
22 12 15 4 34 1 39 42 32 40 7 35 20 29 11 25 9 6 24 26 37 19 17 16 8 21 14 38 27 28 3 33 30 31 18 5 36 13 10 41 23 2
40 1 38 20 29 12 28 18 26 21 23 16 24 37 6 41 15 19 27 8 3 39 22 9 7 17 10 33 4 35 31 25 2 30 36 34 14 11 13 42 32 5
</pre>
;part 4
Generating 1000 Latin Squares of order 256 takes about 1.5secs
<lang fsharp>
printfn "%d" (Array2D.length1 (Seq.item 999 (randLS 256)))
</lang>
{{out}}
<pre>
256
Real: 00:00:01.512, CPU: 00:00:01.970, GC gen0: 10, gen1: 10
</pre>
|