How to resolve the algorithm Latin Squares in reduced form step by step in the F# programming language
How to resolve the algorithm Latin Squares in reduced form step by step in the F# programming language
Table of Contents
Problem Statement
A Latin Square is in its reduced form if the first row and first column contain items in their natural order. The order n is the number of items. For any given n there is a set of reduced Latin Squares whose size increases rapidly with n. g is a number which identifies a unique element within the set of reduced Latin Squares of order n. The objective of this task is to construct the set of all Latin Squares of a given order and to provide a means which given suitable values for g any element within the set may be obtained. For a reduced Latin Square the first row is always 1 to n. The second row is all Permutations/Derangements of 1 to n starting with 2. The third row is all Permutations/Derangements of 1 to n starting with 3 which do not clash (do not have the same item in any column) with row 2. The fourth row is all Permutations/Derangements of 1 to n starting with 4 which do not clash with rows 2 or 3. Likewise continuing to the nth row. Demonstrate by:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Latin Squares in reduced form step by step in the F# programming language
Source code in the fsharp programming language
// Generate Latin Squares in reduced form. Nigel Galloway: July 10th., 2019
let normLS α=
let N=derange α|>List.ofSeq|>List.groupBy(fun n->n.[0])|>List.sortBy(fun(n,_)->n)|>List.map(fun(_,n)->n)|>Array.ofList
let rec fG n g=match n with h::t->fG t (g|>List.filter(fun g->Array.forall2((<>)) h g )) |_->g
let rec normLS n g=seq{for i in fG n N.[g] do if g=α-2 then yield [|1..α|]::(List.rev (i::n)) else yield! normLS (i::n) (g+1)}
match α with 1->seq[[[|1|]]] |2-> seq[[[|1;2|];[|2;1|]]] |_->Seq.collect(fun n->normLS [n] 1) N.[0]
normLS 4 |> Seq.iter(fun n->List.iter(printfn "%A") n;printfn "");;
let rec fact n g=if n<2 then g else fact (n-1) n*g
[1..6] |> List.iter(fun n->let nLS=normLS n|>Seq.length in printfn "order=%d number of Reduced Latin Squares nLS=%d nLS*n!*(n-1)!=%d" n nLS (nLS*(fact n 1)*(fact (n-1) 1)))
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the OCaml programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Fortran programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Microsoft Small Basic programming language
You may also check:How to resolve the algorithm Brilliant numbers step by step in the Sidef programming language