How does fannkuch work?

I am having trouble understanding the instructions for implementing Fannkuch. Instructions: http://www.haskell.org/haskellwiki/Shootout/Fannkuch

After the step โ€œCount the number of flips, here is 5.โ€, Iโ€™m lost.

+7
algorithm
source share
2 answers

Wow, yes, this is not the best description of the algorithm :).

My interpretation is that they want you to do the following:

 fannkuch (n) {
  int maxFlips = 0, printCount = 0;
  foreach permutation p of [1..n] {
   maxFlips = max (maxFlips, flipCount (p));
   if (printCount ++ & lt 30) printPermutation (p);
  }
  print (maxFlips);
 }

 flipCount (p) {
  int count = 0;
  while (p [0]! = 1) {
   reverse (p, p + p [0]);
   count ++;
  }
  return count;
 }
+5
source share

Watch Game Benchmarks - Fannkuch

Requiring the first 30 permutations to be printed was an easy way to limit the permissible changes that can be made to the algorithm.

-2
source share

All Articles