github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/test/bench/shootout/fannkuch.go (about)

     1  /*
     2  Redistribution and use in source and binary forms, with or without
     3  modification, 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  
    17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    20  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    27  POSSIBILITY OF SUCH DAMAGE.
    28  */
    29  
    30  /*
    31   * The Computer Language Benchmarks Game
    32   * http://shootout.alioth.debian.org/
    33   *
    34   * contributed by The Go Authors.
    35   * Based on fannkuch.c by Heiner Marxen
    36   */
    37  
    38  package main
    39  
    40  import (
    41  	"flag"
    42  	"fmt"
    43  )
    44  
    45  var n = flag.Int("n", 7, "count")
    46  
    47  func fannkuch(n int) int {
    48  	if n < 1 {
    49  		return 0
    50  	}
    51  
    52  	n1 := n - 1
    53  	perm := make([]int, n)
    54  	perm1 := make([]int, n)
    55  	count := make([]int, n)
    56  
    57  	for i := 0; i < n; i++ {
    58  		perm1[i] = i // initial (trivial) permutation
    59  	}
    60  
    61  	r := n
    62  	didpr := 0
    63  	flipsMax := 0
    64  	for {
    65  		if didpr < 30 {
    66  			for i := 0; i < n; i++ {
    67  				fmt.Printf("%d", 1+perm1[i])
    68  			}
    69  			fmt.Printf("\n")
    70  			didpr++
    71  		}
    72  		for ; r != 1; r-- {
    73  			count[r-1] = r
    74  		}
    75  
    76  		if perm1[0] != 0 && perm1[n1] != n1 {
    77  			flips := 0
    78  			for i := 1; i < n; i++ { // perm = perm1
    79  				perm[i] = perm1[i]
    80  			}
    81  			k := perm1[0] // cache perm[0] in k
    82  			for {         // k!=0 ==> k>0
    83  				for i, j := 1, k-1; i < j; i, j = i+1, j-1 {
    84  					perm[i], perm[j] = perm[j], perm[i]
    85  				}
    86  				flips++
    87  				// Now exchange k (caching perm[0]) and perm[k]... with care!
    88  				j := perm[k]
    89  				perm[k] = k
    90  				k = j
    91  				if k == 0 {
    92  					break
    93  				}
    94  			}
    95  			if flipsMax < flips {
    96  				flipsMax = flips
    97  			}
    98  		}
    99  
   100  		for ; r < n; r++ {
   101  			// rotate down perm[0..r] by one
   102  			perm0 := perm1[0]
   103  			for i := 0; i < r; i++ {
   104  				perm1[i] = perm1[i+1]
   105  			}
   106  			perm1[r] = perm0
   107  			count[r]--
   108  			if count[r] > 0 {
   109  				break
   110  			}
   111  		}
   112  		if r == n {
   113  			return flipsMax
   114  		}
   115  	}
   116  	return 0
   117  }
   118  
   119  func main() {
   120  	flag.Parse()
   121  	fmt.Printf("Pfannkuchen(%d) = %d\n", *n, fannkuch(*n))
   122  }