1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function fannkuch() { var count = Array(8); var r = 8; var done = 0; while (done < 40) { // write-out the first 30 permutations done += r; while (r != 1) { count[r - 1] = r; r--; } while (true) { count[r] = count[r] - 1; if (count[r] > 0) break; r++; } } return done; } assertEq(fannkuch(), 41);