test178

Undocumented in source.
immutable
auto test178 = q{--- test178 struct Result { i32 sum; i32 maxflips; } // https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/fannkuchredux.html Result fannkuch(i32 n) { i32 signx; i32 maxflips; i32 sum; i32 i; i32 j; i32 k; i32 q1; i32 flips; i32 qq; i32 t; i32 sx; i32 tt; i32[100] p; i32[100] q; i32[100] s; signx = 1; maxflips = 0; sum = 0; for (i=1; i<=n; ++i) { p[i-1] = i; q[i-1] = i; s[i-1] = i; } while (true) { q1 = p[1-1]; if (q1 != 1) { for (i=2; i<=n; ++i) { q[i-1] = p[i-1]; } flips = 1; while (true) { qq = q[q1-1]; if (qq == 1) { sum += signx*flips; if (flips > maxflips) { maxflips = flips; } break; } q[q1-1] = q1; if (q1 >= 4) { i = 2; j = q1-1; while (true) { { i32 temp = q[i-1]; q[i-1] = q[j-1]; q[j-1] = temp; } ++i; --j; if (i >= j) break; } } q1 = qq; ++flips; } } if (signx == 1) { { i32 temp = p[2-1]; p[2-1] = p[1-1]; p[1-1] = temp; } signx = -1; } else { { i32 temp = p[2-1]; p[2-1] = p[3-1]; p[3-1] = temp; } signx = 1; for (i=3; i<=n; ++i) { sx = s[i-1]; if (sx != 1) { s[i-1] = sx-1; break; } if (i == n) { return Result(sum, maxflips); } s[i-1] = i; tt = p[1-1]; for (j=1; j<=i; ++j) { p[j-1] = p[j+1-1]; } p[i+1-1] = tt; } } } } };

Meta