37 lines
795 B
Plaintext
37 lines
795 B
Plaintext
const MAX = 12;
|
|
var super=Data(), pos, cnt; // global state, ick
|
|
|
|
fcn fact_sum(n){ // -->1! + 2! + ... + n!
|
|
[1..n].reduce(fcn(s,n){ s + [2..n].reduce('*,1) },0)
|
|
}
|
|
|
|
fcn r(n){
|
|
if (not n) return(0);
|
|
|
|
c := super[pos - n];
|
|
if (not (cnt[n]-=1)){
|
|
cnt[n] = n;
|
|
if (not r(n-1)) return(0);
|
|
}
|
|
super[pos] = c; pos+=1;
|
|
1
|
|
}
|
|
|
|
fcn superperm(n){
|
|
pos = n;
|
|
len := fact_sum(n);
|
|
super.fill(0,len); // this is pretty close to recalloc()
|
|
|
|
cnt = (n+1).pump(List()); //-->(0,1,2,3,..n)
|
|
foreach i in (n){ super[i] = i + 0x31; } //-->"1" ... "123456789:;"
|
|
while (r(n)){}
|
|
}
|
|
|
|
foreach n in (MAX){
|
|
superperm(n);
|
|
print("superperm(%2d) len = %d".fmt(n,super.len()));
|
|
// uncomment next line to see the string itself
|
|
//print(": %s".fmt(super.text));
|
|
println();
|
|
}
|