Skip to content

Instantly share code, notes, and snippets.

@adam-singer
Forked from gozzoo/fannkuchredux.dart
Created July 6, 2012 03:47
Show Gist options
  • Save adam-singer/3057941 to your computer and use it in GitHub Desktop.
Save adam-singer/3057941 to your computer and use it in GitHub Desktop.
fannkuchredux benchmark implementation in dart
#import('dart:core');
class FannkuchreduxTest {
static int fannkuchredux(int n) {
var perm = new List(n);
var perm1 = new List(n);
var count = new List(n);
int maxFlipsCount = 0;
int permCount = 0;
int checksum = 0;
int i;
for (i = 0; i < n; i++)
perm1[i] = i;
int r = n;
while (true) {
while (r != 1) {
count[r-1] = r;
r -= 1;
}
for (i = 0; i < n; i += 1)
perm[i] = perm1[i];
int flipsCount = 0;
for (int k = perm[0]; k != 0; k = perm[0]) {
int k2 = (k+1) >> 1;
for (i = 0; i < k2; i++) {
int temp = perm[i];
perm[i] = perm[k-i];
perm[k-i] = temp;
}
flipsCount += 1;
}
if (flipsCount > maxFlipsCount)
maxFlipsCount = flipsCount;
int csi = flipsCount;
if (permCount % 2 != 0)
csi = -flipsCount;
checksum += csi;
while (true) {
if (r == n) {
print("$checksum");
return maxFlipsCount;
}
int perm0 = perm1[0];
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++;
}
//return 0;
}
}
void main() {
int n = 11;
int t1 = Clock.now();
int res = FannkuchreduxTest.fannkuchredux(n);
print("Pfannkuchen($n) = $res\n");
int t2 = Clock.now();
double t = (t2 - t1) / 1000;
print("time: $t");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment