3 /* The Great Computer Language Shootout
4 http://shootout.alioth.debian.org/
5 contributed by Ian Osgood */
9 for (i
,1,n
, a
append(i
))
12 count
:= maxFlips
:= 0
15 // display the first
30 permutations
16 if (count
< 30, writeln(a
join("")); count
= count
+ 1)
18 // eliminate bad choices
19 if (a first
!= 1 and a last
!= n
,
24 while ((j
:= p first
) > 1,
27 while ((i
=i
+1) < (j
=j
-1), p
swapIndices(i
,j
))
30 if (flips
> maxFlips
, maxFlips
= flips
)
33 // generate another permutation
34 while (r
>1, counts
atPut(r
-1, r
); r
=r
-1)
37 a
atInsert(r
, a
removeAt(0))
39 if (counts
atPut(r
, counts
at(r
) - 1) > 0, break)
41 if ((r
=r
+1) == n
, return maxFlips
)
46 n
:= System args
at(1) asNumber
48 writeln("Pfannkuchen(", n
, ") = ", f
)