The Computer Language
Benchmarks Game

fannkuch-redux C# .NET Core program

source code

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

   contributed by Isaac Gouy, transliterated from Rex Kerr's Scala program
*/

using System;

class FannkuchRedux
{
   public static int fannkuch(int n) {
      int[] perm = new int[n], perm1 = new int[n], count = new int[n];
      for(int j=0; j<n; j++) perm1[j] = j;
      int 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) {
             var 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) {
               Console.WriteLine(checksum);
               return flips;
            }
            var p0 = perm1[0];
            i = 0;
            while (i < r) {
               var 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;
   }

   static void Main(string[] args){
      int n = 7;
      if (args.Length > 0) n = Int32.Parse(args[0]);
      Console.WriteLine("Pfannkuchen({0}) = {1}", n, fannkuch(n));
   }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true


Thu, 26 Oct 2017 23:57:46 GMT

MAKE:
cp fannkuchredux.csharpcore Program.cs
cp Include/csharpcore/tmp.csproj .
cp Include/csharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/csharpcore/tmp.csproj.nuget.g.props ./obj
cp Include/csharpcore/tmp.csproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/benchmarksgame_quadcore/fannkuchredux/tmp/bin/Release/netcoreapp2.0/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.54

6.11s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/tmp.dll 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65