The Computer Language
Benchmarks Game

fannkuch-redux Scala program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   conversion to Scala by Rex Kerr
   from Java version by Oleg Mazurov and Isaac Gouy
*/

object fannkuchredux
{
  def fannkuch(n: Int): Int = {
    val perm1 = Array.range(0,n)
    val perm, count = new Array[Int](n)
    var f, i, k, r, flips, nperm, 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) {
          val 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
      var go = true
      while (go) {
        if (r == n) {
          println(checksum)
          return flips
        }
        val p0 = perm1(0)
        i = 0
        while (i < r) {
          val 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
    }
    flips
  }

  def main(args: Array[String]) {
    val n = (if (args.length > 0) args(0).toInt else 7)
    println("Pfannkuchen("+n+") = "+fannkuch(n))
  }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Scala compiler version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


Mon, 06 Feb 2017 05:12:13 GMT

MAKE:
mv fannkuchredux.scala fannkuchredux.scala
mv: 'fannkuchredux.scala' and 'fannkuchredux.scala' are the same file
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:600: recipe for target 'fannkuchredux.scala_run' failed
make: [fannkuchredux.scala_run] Error 1 (ignored)
/usr/local/src/scala-2.12.1/bin/scalac -optimise -target:jvm-1.8 fannkuchredux.scala
warning: there was one deprecation warning; re-run with -deprecation for details
one warning found
5.25s to complete and log all make actions

COMMAND LINE:
 /usr/local/src/jdk1.8.0_121/bin/java  -Xbootclasspath/a:/usr/local/src/scala-2.12.1/lib/scala-library.jar:/usr/local/src/scala-2.12.1/lib/scala-actors-2.11.0.jar:/usr/local/src/scala-2.12.1/lib/akka-actor_2.11-2.3.4.jar:/usr/local/src/scala-2.12.1/lib/config-1.2.1.jar fannkuchredux 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65