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

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -