2 * The Computer Lannguage Shootout
3 * http://shootout.alioth.debian.org/
4 * Contributed by Heiner Marxen
33 perm = calloc(n, sizeof(*perm ));
34 perm1 = calloc(n, sizeof(*perm1));
35 count = calloc(n, sizeof(*count));
37 for( i=0 ; i<n ; ++i ) perm1[i] = i; /* initial (trivial) permu */
39 r = n; didpr = 0; flipsMax = 0;
42 for( i=0 ; i<n ; ++i ) printf("%d", (int)(1+perm1[i]));
50 #define XCH(x,y) { Aint t_mp; t_mp=(x); (x)=(y); (y)=t_mp; }
52 if( ! (perm1[0]==0 || perm1[n1]==n1) ) {
54 for( i=1 ; i<n ; ++i ) { /* perm = perm1 */
57 k = perm1[0]; /* cache perm[0] in k */
58 do { /* k!=0 ==> k>0 */
60 for( i=1, j=k-1 ; i<j ; ++i, --j ) {
65 * Now exchange k (caching perm[0]) and perm[k]... with care!
66 * XCH(k, perm[k]) does NOT work!
68 j=perm[k]; perm[k]=k ; k=j;
70 if( flipsMax < flips ) {
79 /* rotate down perm[0..r] by one */
90 if( (count[r] -= 1) > 0 ) {
99 main( int argc, char* argv[] )
101 int n = (argc>1) ? atoi(argv[1]) : 10;
103 printf("Pfannkuchen(%d) = %ld\n", n, fannkuch(n));