The Computer Language
24.11 Benchmarks Game

fannkuch-redux C# .NET #2 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   contributed by Isaac Gouy, transliterated from Mike Pall's Lua program 
*/

using System;

class FannkuchRedux
{
   public static int[] fannkuch(int n) {
      int[] p = new int[n], q = new int[n], s = new int[n]; 
      int sign = 1, maxflips = 0, sum = 0, m = n-1;
      for(int i=0; i<n; i++){ p[i] = i; q[i] = i; s[i] = i; }      
      do {
         // Copy and flip.
         var q0 = p[0];                                     // Cache 0th element.
         if (q0 != 0){
            for(int i=1; i<n; i++) q[i] = p[i];             // Work on a copy.
            var flips = 1;
            do { 
               var qq = q[q0]; 
               if (qq == 0){                                // ... until 0th element is 0.
                  sum += sign*flips;
	          if (flips > maxflips) maxflips = flips;   // New maximum?
                  break; 
               } 
 	       q[q0] = q0; 
	       if (q0 >= 3){
	          int i = 1, j = q0 - 1, t;
                  do { t = q[i]; q[i] = q[j]; q[j] = t; i++; j--; } while (i < j); 
               }
	       q0 = qq; flips++;
            } while (true); 
         }
         // Permute.
         if (sign == 1){
            var t = p[1]; p[1] = p[0]; p[0] = t; sign = -1; // Rotate 0<-1.
         } else { 
            var t = p[1]; p[1] = p[2]; p[2] = t; sign = 1;  // Rotate 0<-1 and 0<-1<-2.
            for(int i=2; i<n; i++){ 
	       var sx = s[i];
	       if (sx != 0){ s[i] = sx-1; break; }
	       if (i == m) return new int[]{sum,maxflips};  // Out of permutations.
	       s[i] = i;
	       // Rotate 0<-...<-i+1.
	       t = p[0]; for(int j=0; j<=i; j++){ p[j] = p[j+1]; } p[i+1] = t;
            }
         }
      } while (true);
   }

   static void Main(string[] args){
      int n = (args.Length > 0) ? Int32.Parse(args[0]) : 7;
      var pf = fannkuch(n);
      Console.Write("{0}\nPfannkuchen({1}) = {2}\n", pf[0], n, pf[1]);
   }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 9.0.100
Host Version: 9.0.0
Commit: 9d5a6a9aa4

<OutputType>Exe
<TargetFramework>net9.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false



 Wed, 13 Nov 2024 22:10:29 GMT

MAKE:
cp fannkuchredux.csharpcore-2.csharpcore Program.cs
cp Include/csharpcore/program.csproj .
mkdir obj
cp Include/csharpcore/project.assets.json ./obj
/opt/src/dotnet-sdk-9.0.100/dotnet build -c Release --use-current-runtime  	
  Determining projects to restore...
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
  Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/program.csproj (in 6.13 sec).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
  program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/bin/Release/net9.0/linux-x64/program.dll

Build succeeded.

/home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:11.07

13.04s to complete and log all make actions

COMMAND LINE:
 ./bin/Release/net9.0/linux-x64/program 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65