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