希望組成為八位數 e.g. "12344321"
重複排列 8! = 8*7*6*5*4*3*2*1 = 40320
不重複排列 8!/(2!*2!*2!*2!) = 8!/16 = 2520
不重複排列公式為 (P! / (n1! * n2! * n3! * n4!))
而 1, 1, 2, 2, 3, 3, 4, 4,所組成的 2520 數字,都無法被開根號 (sqrt)
PHP 排列組合 EXAMPLE 如下
Code: Select all
<?PHP
function wordcombos($words)
{
if ( count($words) <= 1 )
{
$result = $words;
} else {
$result = array();
for ( $i = 0; $i < count($words); ++$i ) {
$firstword = $words[$i];
$remainingwords = array();
for ( $j = 0; $j < count($words); ++$j ) {
if ( $i <> $j ) $remainingwords[] = $words[$j];
}
$combos = wordcombos($remainingwords);
for ( $j = 0; $j < count($combos); ++$j ) {
$result[] = $firstword . ' ' . $combos[$j];
}
}
}
return $result;
}
$test_word = array('1', '1', '2', '2', '3', '3', '4', '4');
// $test_word = array('1', '1', '2', '2', '3', '3');
// $test_word = array('1', '1', '2', '2', '3', '3', '4', '4');
$arrayresult = array();
$arrayresult = array_unique(wordcombos($test_word));
$i = 1;
foreach ($arrayresult as $combo)
{
$n0=intval(str_replace(' ', '', $combo));
$nflt=sqrt($n0) + 0.0;
// $nint=intval(sqrt($n0));
echo '#'."$i SQRT($n0)=$nflt\n";
$i++;
}
?>