The Computer Language
24.11 Benchmarks Game

fannkuch-redux C# .NET #8 program

source code

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

   Naive transliteration from Rex Kerr's Scala program
   contributed by Isaac Gouy 
*/

class FannkuchRedux {
   public static int fannkuch(int n) {
      int i, k, r;    
      var perm1 = new int[n];   
      for (i = 0; i < n; i++) perm1[i] = i;      
      var perm = new int[n];
      var count = new int[n];       
      int f = 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) {          
               int 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 more = true;
         while (more) {   
            if (r == n) {
               Console.WriteLine(checksum);               
               return flips; 
            }
            int p0 = perm1[0];
            i = 0;
            while (i < r) {
               int j = i + 1;
               perm1[i] = perm1[j];
               i = j;            
            }
            perm1[r] = p0; 
         
            count[r] -= 1;         
            if (count[r] > 0) more = false; else r += 1;  
         }
         nperm += 1;
      }
      return flips;      
   }
   
   static void Main(String[] args){      
      int n = (args.Length > 0) 
         ? Int32.Parse(args[0]) 
         : 7;
      
      Console.Write("Pfannkuchen({0}) = {1}\n", n, fannkuch(n));      
   }
}
    

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:15:38 GMT

MAKE:
cp fannkuchredux.csharpcore-8.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.04 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:10.99

12.96s to complete and log all make actions

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

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65