fannkuch-redux Java naot #2 program
    
   
  
    
      
source code
    
    
/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
   contributed by Isaac Gouy
   converted to Java by Oleg Mazurov
*/
public class fannkuchredux
{
   public static int fannkuch(int n) {
      int[] perm = new int[n];
      int[] perm1 = new int[n];
      int[] count = new int[n];
      int maxFlipsCount = 0;
      int permCount = 0;
      int checksum = 0;
      for(int i=0; i<n; i++) perm1[i] = i;
      int r = n;
      while (true) {
         while (r != 1){ count[r-1] = r; r--; }
         for(int i=0; i<n; i++) perm[i] = perm1[i];
         int flipsCount = 0;
         int k;
         while ( !((k=perm[0]) == 0) ) {
            int k2 = (k+1) >> 1;
            for(int i=0; i<k2; i++) {
               int temp = perm[i]; perm[i] = perm[k-i]; perm[k-i] = temp;
            }
            flipsCount++;
         }
         maxFlipsCount = Math.max(maxFlipsCount, flipsCount);
         checksum += permCount%2 == 0 ? flipsCount : -flipsCount;
         // Use incremental change to generate another permutation
         while (true) {
            if (r == n) {
	       System.out.println( checksum );
	       return maxFlipsCount;
	    }
            int perm0 = perm1[0];
            int i = 0;
            while (i < r) {
               int j = i + 1;
               perm1[i] = perm1[j];
               i = j;
            }
            perm1[r] = perm0;
            count[r] = count[r] - 1;
            if (count[r] > 0) break;
            r++;
         }
         permCount++;
      }
   }
   public static void main(String[] args){
      int n = 7;
      if (args.length > 0) n = Integer.parseInt(args[0]);
      System.out.println("Pfannkuchen("+n+") = "+fannkuch(n));
   }
}
    
  
  
    
      
notes, command-line, and program output
    
    
NOTES:
64-bit Ubuntu quad core
native-image 23.0.2 2025-01-21
GraalVM Runtime Environment
Oracle GraalVM 23.0.2+7.1
 Mon, 17 Feb 2025 01:16:36 GMT
MAKE:
mv fannkuchredux.graalvmaot-2.graalvmaot fannkuchredux.java
/opt/src/graalvm-jdk-23/bin/javac -d . -cp .  fannkuchredux.java
/opt/src/graalvm-jdk-23/bin/native-image --silent --gc=G1 -cp . -O3 -march=native fannkuchredux -o fannkuchredux.graalvmaot-2.graalvmaot_run
95.31 seconds to complete and log all make actions
COMMAND LINE:
 ./fannkuchredux.graalvmaot-2.graalvmaot_run 12
PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65