1/*
2Redistribution and use in source and binary forms, with or without
3modification, are permitted provided that the following conditions are met:
4
5    * Redistributions of source code must retain the above copyright
6    notice, this list of conditions and the following disclaimer.
7
8    * Redistributions in binary form must reproduce the above copyright
9    notice, this list of conditions and the following disclaimer in the
10    documentation and/or other materials provided with the distribution.
11
12    * Neither the name of "The Computer Language Benchmarks Game" nor the
13    name of "The Computer Language Shootout Benchmarks" nor the names of
14    its contributors may be used to endorse or promote products derived
15    from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28*/
29
30/*
31 * The Computer Language Shootout
32 * http://shootout.alioth.debian.org/
33 * Contributed by Heiner Marxen
34 *
35 * "fannkuch"	for C gcc
36 *
37 * $Id: fannkuch.1.gcc.code,v 1.15 2009-04-28 15:39:31 igouy-guest Exp $
38 */
39
40#include <stdio.h>
41#include <stdlib.h>
42
43#define Int	int
44#define Aint	int
45
46    static long
47fannkuch( int n )
48{
49    Aint*	perm;
50    Aint*	perm1;
51    Aint*	count;
52    long	flips;
53    long	flipsMax;
54    Int		r;
55    Int		i;
56    Int		k;
57    Int		didpr;
58    const Int	n1	= n - 1;
59
60    if( n < 1 ) return 0;
61
62    perm  = calloc(n, sizeof(*perm ));
63    perm1 = calloc(n, sizeof(*perm1));
64    count = calloc(n, sizeof(*count));
65
66    for( i=0 ; i<n ; ++i ) perm1[i] = i;	/* initial (trivial) permu */
67
68    r = n; didpr = 0; flipsMax = 0;
69    for(;;) {
70	if( didpr < 30 ) {
71	    for( i=0 ; i<n ; ++i ) printf("%d", (int)(1+perm1[i]));
72	    printf("\n");
73	    ++didpr;
74	}
75	for( ; r!=1 ; --r ) {
76	    count[r-1] = r;
77	}
78
79#define XCH(x,y)	{ Aint t_mp; t_mp=(x); (x)=(y); (y)=t_mp; }
80
81	if( ! (perm1[0]==0 || perm1[n1]==n1) ) {
82	    flips = 0;
83	    for( i=1 ; i<n ; ++i ) {	/* perm = perm1 */
84		perm[i] = perm1[i];
85	    }
86	    k = perm1[0];		/* cache perm[0] in k */
87	    do {			/* k!=0 ==> k>0 */
88		Int	j;
89		for( i=1, j=k-1 ; i<j ; ++i, --j ) {
90		    XCH(perm[i], perm[j])
91		}
92		++flips;
93		/*
94		 * Now exchange k (caching perm[0]) and perm[k]... with care!
95		 * XCH(k, perm[k]) does NOT work!
96		 */
97		j=perm[k]; perm[k]=k ; k=j;
98	    }while( k );
99	    if( flipsMax < flips ) {
100		flipsMax = flips;
101	    }
102	}
103
104	for(;;) {
105	    if( r == n ) {
106		return flipsMax;
107	    }
108	    /* rotate down perm[0..r] by one */
109	    {
110		Int	perm0 = perm1[0];
111		i = 0;
112		while( i < r ) {
113		    k = i+1;
114		    perm1[i] = perm1[k];
115		    i = k;
116		}
117		perm1[r] = perm0;
118	    }
119	    if( (count[r] -= 1) > 0 ) {
120		break;
121	    }
122	    ++r;
123	}
124    }
125}
126
127    int
128main( int argc, char* argv[] )
129{
130    int		n = (argc>1) ? atoi(argv[1]) : 0;
131
132    printf("Pfannkuchen(%d) = %ld\n", n, fannkuch(n));
133    return 0;
134}
135