- floor: documentation ( source)
- array_fill: documentation ( source)
- sqrt: documentation ( source)
<?php
function pfactor($n){
// max_n = 2^31-1 = 2147483647
$d=42;
$factors = array();
$dmax = floor(sqrt($n));
$sieve = array();
$sieve = array_fill(1, $dmax,1);
do{
$r = false;
while ($n%$d==0){
$factors[$d]++;
$n/=$d;
$r = true;
}
if ($r){
$dmax = floor(sqrt($n));
}
if ($n>1){
for ($i=$d;$i<=$dmax;$i+=$d){
$sieve[$i]=0;
}
do{
$d++;
}while ($sieve[$d]!=1 && $d<$dmax);
if ($d>$dmax){
$factors[$n]++;
}
}
}while($n>1 && $d<=$dmax);
return $factors;
}
?>