The Computer Language
Benchmarks Game

fannkuch-redux TypeScript #2 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
   contributed by Isaac Gouy 
*/

/// <reference path="/usr/local/src/typescript/node.d.ts" />


function fannkuch(n: number): number {
   const perm = new Int32Array(n), count = new Int32Array(n)

   const perm1 = new Int32Array(n)
   for (let i=0; i<n; i++) { perm1[i] = i }

   let f = 0, i = 0, k = 0, r = 0, flips = 0, nperm = 0, checksum = 0

   r = n
   while (r > 0) {
      i = 0
      while (r != 1) { count[r-1] = r; r -= 1 }
      while (i < n) { perm[i] = perm1[i]; i += 1 }

      // Count flips and update max  and checksum
      f = 0
      k = perm[0]
      while (k != 0) {
         i = 0
         while (2*i < k) {
            const t = perm[i]; perm[i] = perm[k-i]; perm[k-i] = t
            i += 1
         }
         k = perm[0]
         f += 1
      }
      if (f > flips) { flips = f }
      if ((nperm & 0x1) == 0) { checksum += f } else { checksum -= f }

      // Use incremental change to generate another permutation
      let go = true
      while (go) {
         if (r == n) {
            console.log(checksum)
            return flips
         }
         let p0 = perm1[0]
         i = 0
         while (i < r) {
            const j = i+1
            perm1[i] = perm1[j]
            i = j
         }
         perm1[r] = p0

         count[r] -= 1
         if (count[r] > 0) { go = false } else { r += 1 }
      }
      nperm += 1
   }
   return flips
}


const n = +process.argv[2]
console.log( "Pfannkuchen(" + n + ") = " + fannkuch(n) )

    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Version 2.0.3
node.js v6.9.0


Wed, 19 Oct 2016 19:24:20 GMT

MAKE:
mv fannkuchredux.typescript-2.typescript fannkuchredux.typescript-2.ts
/usr/local/src/node-v6.9.0-linux-x86/bin/tsc  fannkuchredux.typescript-2.ts
../../../../../usr/local/src/typescript/node.d.ts(393,11): error TS2430: Interface 'NodeBuffer' incorrectly extends interface 'Uint8Array'.
  Types of property 'fill' are incompatible.
    Type '(value: any, offset?: number, end?: number) => Buffer' is not assignable to type '(value: number, start?: number, end?: number) => this'.
      Type 'Buffer' is not assignable to type 'this'.
/home/dunham/benchmarksgame/nanobench/makefiles/u32.programs.Makefile:601: recipe for target 'fannkuchredux.typescript-2.typescript_run' failed
make: [fannkuchredux.typescript-2.typescript_run] Error 2 (ignored)
/usr/local/src/node-v6.3.0-linux-x86/lib/node_modules/babel-cli/bin/babel.js --plugins transform-es2015-modules-commonjs fannkuchredux.typescript-2.js -o fannkuchredux.typescript-2.js
make: /usr/local/src/node-v6.3.0-linux-x86/lib/node_modules/babel-cli/bin/babel.js: Command not found
/home/dunham/benchmarksgame/nanobench/makefiles/u32.programs.Makefile:601: recipe for target 'fannkuchredux.typescript-2.typescript_run' failed
make: [fannkuchredux.typescript-2.typescript_run] Error 127 (ignored)
2.67s to complete and log all make actions

COMMAND LINE:
/usr/local/src/node-v6.9.0-linux-x86/bin/node --use_strict fannkuchredux.typescript-2.js 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65