How to decompose a natural number into prime factors using PHP
?
At the moment there is such code:
function factors($n = 0, $array = FALSE) { $pf = array(); for ($i = 2; $i <= $n / $i; $i++) { while ($n % $i == 0) { $pf[] = $i; $n = $n / $i; } } if ($n > 1) $pf[] = $n; return ($array === TRUE) ? $pf : implode(' * ', $pf); }
Can you do better?