Superpermutation minimisation: Difference between revisions

Line 600:
</pre>
 
=={{header|Objeck}}==
{{trans|C}}
<lang objeck>class SuperPermutation {
@super : static : Char[];
@pos : static : Int;
@cnt : static : Int[];
 
function : Main(args : String[]) ~ Nil {
max := 12;
@cnt := Int->New[max];
@super := Char->New[0];
 
for(n := 0; n < max; n += 1;) {
"superperm({$n}) "->Print();
SuperPerm(n);
len := @super->Size() - 1;
"len = {$len}"->PrintLine();
};
}
 
function : native : FactSum(n : Int) ~ Int {
s := 0; x := 0; f := 1;
while(x < n) {
f *= ++x; s += f;
};
return s;
}
 
function : native : R(n : Int) ~ Bool {
if(n = 0) {
return false;
};
 
c := @super[@pos - n];
if(--@cnt[n] = 0) {
@cnt[n] := n;
if(<>R(n - 1)) {
return false;
};
};
@super[@pos++] := c;
return true;
}
 
function : native : SuperPerm(n : Int) ~ Nil {
@pos := n;
len := FactSum(n);
 
tmp := Char->New[len + 1];
for(i := 0; i < @super->Size(); i += 1;) {
tmp[i] := @super[i];
};
@super := tmp;
 
for(i := 0; i <= n; i += 1;) {
@cnt[i] := i;
};
 
for(i := 1; i <= n; i += 1;) {
@super[i - 1] := i + '0';
};
do {
r := R(n);
}
while(r);
}
}
</lang>
 
{{output}}
<pre>
superperm(0) len = 0
superperm(1) len = 1
superperm(2) len = 3
superperm(3) len = 9
superperm(4) len = 33
superperm(5) len = 153
superperm(6) len = 873
superperm(7) len = 5913
superperm(8) len = 46233
superperm(9) len = 409113
superperm(10) len = 4037913
superperm(11) len = 43954713
</pre>
=={{header|Perl}}==
This uses a naive method of just concatenating the new permutation to the end (or prepending to the front) if it is not already in the string. Adding to the end is similar to Python's '''s_perm1()''' function.
760

edits