code to find factors in PHP does not work for negative numbers -
the code below works factor(6) instance. however, factor(-8), code return blank page. have tried lot, cannot find out wrong
function factor($n){ ($x = 1; $x <= sqrt($n); $x++) { if ($n % $x == 0) { $z = $n/$x; echo "$x , $z"."<br><br>"; } } }
you want avoid imaginary numbers - in loop. seems php doesn't handle well.
make $n positive , function work:
function factor($n){ ($x = 1; $x <= sqrt(abs($n)); $x++) { if ($n % $x == 0) { $z = $n/$x; echo "$x , $z"."<br><br>"; } } }
Comments
Post a Comment