PHP code[User:root Time:21:21:46 Path:/home/liangdong/php]$ php permutation.php
Total time used : 9738642ms
[User:root Time:21:21:56 Path:/home/liangdong/php]$ cat permutation.php
$beg_time = microtime(true);
$beg_time *= 1000 * 1000;
$table = range(1, 33);
$temp = array();
$fp = null;
function _get_permutations($index, $num) {
global $temp;
global $total;
global $table;
global $fp;
if ($num == $total) {
global $perms;
fwrite($fp, json_encode($temp) . PHP_EOL);
return;
}
for ($ndx = $index; 33 - $ndx + $num >= $total; ++ $ndx) {
$temp[$num] = $table[$ndx];
_get_permutations($ndx + 1, $num + 1);
}
}
function get_permutations($n = 6) {
global $total;
global $perms;
global $fp;
if ($n <= 0 && $n > 33) {
return false;
}
$total = $n;
$fp = fopen("data.txt", "w");
_get_permutations(0, 0);
fclose($fp);
return true;
}
get_permutations();
$end_time = microtime(true);
$end_time *= 1000 * 1000;
echo "Total time used : " . ($end_time - $beg_time) . "ms" . PHP_EOL;
?>