Jump to content

Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique: Difference between revisions

m
no edit summary
(→‎{{header|Raku}}: fix oversight in squares generation)
mNo edit summary
Line 17:
=={{header|F_Sharp|F#}}==
===The Functions===
<langsyntaxhighlight lang="fsharp">
// Jacobson and Matthews technique for generating Latin Squares. Nigel Galloway: August 5th., 2019
let R=let N=System.Random() in (fun n->N.Next(n))
Line 39:
 
let randLS α=Seq.unfold(fun g->Some(g,jmLS α g))(Array2D.init α α (fun n g->1+(n+g)%α))
</syntaxhighlight>
</lang>
===The Task===
;part 1
<langsyntaxhighlight lang="fsharp">
randLS 4 |> Seq.take 10000 |> Seq.map asNormLS |> Seq.countBy id |> Seq.iter(fun n->printf "%A was produced %d times\n\n" (fst n)(snd n))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 68:
</pre>
;part 2
<langsyntaxhighlight lang="fsharp">
randLS 5 |> Seq.take 10000 |> Seq.map asNormLS |> Seq.countBy id |> Seq.iteri(fun n g->printf "%d(%d) " (n+1) (snd g)); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 76:
</pre>
;part 3
<langsyntaxhighlight lang="fsharp">
let q=Seq.item 749 (randLS 42)
for n in [0..41] do (for g in [0..41] do printf "%3d" q.[n,g]); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 127:
;part 4
Generating 1000 Latin Squares of order 256 takes about 1.5secs
<langsyntaxhighlight lang="fsharp">
printfn "%d" (Array2D.length1 (Seq.item 999 (randLS 256)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 140:
 
Part 4 is taking about 6.5 seconds on my Celeron @1.6 GHz but will be much faster on a more modern machine. Being able to compute random, uniformly distributed, Latin squares of order 256 reasonably quickly is interesting from a secure communications or cryptographic standpoint as the symbols of such a square can represent the 256 characters of the various extended ASCII encodings.
<langsyntaxhighlight lang="go">package main
 
import (
Line 416:
elapsed := time.Since(start)
fmt.Printf("Generated in %s\n", elapsed)
}</langsyntaxhighlight>
 
{{out}}
Line 516:
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">const Cube = Vector{Vector{Vector{Int}}}
const Mat = Vector{Vector{Int}}
 
Line 679:
 
testJacobsenMatthews()
</langsyntaxhighlight>{{out}}
<pre>
PART 1: 10,000 latin Squares of order 4 in reduced form:
Line 776:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat, tables, times
 
type
Line 976:
for _ in 1..1000:
c.shuffle()
echo &"Generated in {cpuTime() - start:.3f} s."</langsyntaxhighlight>
 
{{out}}
Line 1,073:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">shuffleCube</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ry</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rz</span>
Line 1,262:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,336:
=={{header|Raku}}==
{{trans|Go}}
<syntaxhighlight lang="raku" perl6line># 20210729 Raku programming solution
 
#!/usr/bin/env raku
Line 1,443:
for ^100 { shuffleCube @c } # without hyper, will do only 100 cycles
say "Generated in { now - $snapshot } seconds."
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,534:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
import "/seq" for Lst
Line 1,759:
for (i in 1..1000) shuffleCube.call(c)
var elapsed = System.clock - start
Fmt.print("Generated in $s seconds", elapsed)</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.