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