Latin Squares in reduced form: Difference between revisions

m (use wiki markup for links)
Line 1,334:
</pre>
The only way to complete the tasks requirement to produce a table is with another language. Ruby has the ability to run an external program, capture the output, and text handling ability to format it to this tasks requirements. Othe scripting languages are available.
 
=={{header|Perl}}==
It takes a little under 2 minutes to find order 7.
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Latin_Squares_in_reduced_form
use warnings;
 
my $n = 0;
my $count;
our @perms;
 
while( ++$n <= 7 )
{
$count = 0;
@perms = perm( my $start = join '', 1 .. $n );
find( $start );
print "order $n size $count total @{[$count * fact($n) * fact($n-1)]}\n\n";
}
 
sub find
{
@_ >= $n and return $count += ($n != 4) || print join "\n", @_, "\n";
local @perms = grep 0 == ($_[-1] ^ $_) =~ tr/\0//, @perms;
my $row = @_ + 1;
find( @_, $_ ) for grep /^$row/, @perms;
}
 
sub fact { $_[0] > 1 ? $_[0] * fact($_[0] - 1) : 1 }
 
sub perm
{
my $s = shift;
length $s <= 1 ? $s :
map { my $f = $_; map "$f$_", perm( $s =~ s/$_//r ) } split //, $s;
}</lang>
{{out}}
<pre>
order 1 size 1 total 1
 
order 2 size 1 total 2
 
order 3 size 1 total 12
 
1234
2143
3412
4321
 
1234
2143
3421
4312
 
1234
2341
3412
4123
 
1234
2413
3142
4321
 
order 4 size 4 total 576
 
order 5 size 56 total 161280
 
order 6 size 9408 total 812851200
 
order 7 size 16942080 total 61479419904000
</pre>
 
=={{header|Phix}}==
Anonymous user