Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* The Great Computer Language Shootout |
michael@0 | 2 | http://shootout.alioth.debian.org/ |
michael@0 | 3 | contributed by Isaac Gouy */ |
michael@0 | 4 | |
michael@0 | 5 | function fannkuch(n) { |
michael@0 | 6 | var check = 0; |
michael@0 | 7 | var perm = Array(n); |
michael@0 | 8 | var perm1 = Array(n); |
michael@0 | 9 | var count = Array(n); |
michael@0 | 10 | var maxPerm = Array(n); |
michael@0 | 11 | var maxFlipsCount = 0; |
michael@0 | 12 | var m = n - 1; |
michael@0 | 13 | |
michael@0 | 14 | for (var i = 0; i < n; i++) perm1[i] = i; |
michael@0 | 15 | var r = n; |
michael@0 | 16 | |
michael@0 | 17 | while (true) { |
michael@0 | 18 | // write-out the first 30 permutations |
michael@0 | 19 | if (check < 30){ |
michael@0 | 20 | var s = ""; |
michael@0 | 21 | for(var i=0; i<n; i++) s += (perm1[i]+1).toString(); |
michael@0 | 22 | check++; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | while (r != 1) { count[r - 1] = r; r--; } |
michael@0 | 26 | if (!(perm1[0] == 0 || perm1[m] == m)) { |
michael@0 | 27 | for (var i = 0; i < n; i++) perm[i] = perm1[i]; |
michael@0 | 28 | |
michael@0 | 29 | var flipsCount = 0; |
michael@0 | 30 | var k; |
michael@0 | 31 | |
michael@0 | 32 | while (!((k = perm[0]) == 0)) { |
michael@0 | 33 | var k2 = (k + 1) >> 1; |
michael@0 | 34 | for (var i = 0; i < k2; i++) { |
michael@0 | 35 | var temp = perm[i]; perm[i] = perm[k - i]; perm[k - i] = temp; |
michael@0 | 36 | } |
michael@0 | 37 | flipsCount++; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | if (flipsCount > maxFlipsCount) { |
michael@0 | 41 | maxFlipsCount = flipsCount; |
michael@0 | 42 | for (var i = 0; i < n; i++) maxPerm[i] = perm1[i]; |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | while (true) { |
michael@0 | 47 | if (r == n) return maxFlipsCount; |
michael@0 | 48 | var perm0 = perm1[0]; |
michael@0 | 49 | var i = 0; |
michael@0 | 50 | while (i < r) { |
michael@0 | 51 | var j = i + 1; |
michael@0 | 52 | perm1[i] = perm1[j]; |
michael@0 | 53 | i = j; |
michael@0 | 54 | } |
michael@0 | 55 | perm1[r] = perm0; |
michael@0 | 56 | |
michael@0 | 57 | count[r] = count[r] - 1; |
michael@0 | 58 | if (count[r] > 0) break; |
michael@0 | 59 | r++; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | var n = 8; |
michael@0 | 65 | var ret = fannkuch(n); |
michael@0 | 66 | assertEq(ret, 22) |