Robson » Code » PHP » Perfect Number

 
<?
   // perfect number checker v1.0
 
   // returns true if the number is perfect
   // perfect numbers are equal to their proper divisors
   function is_perfect($number)
   {
       for ($n = 2; $n <= sqrt($number); $n++)
       {
           if (!($number % $n))
           {
               $d += $n;
               if ($n <> $number / $n)
                   $d += $number / $n;
           }
       }
       return ++$d == $number;
   }
   
   // * example use - display perfect numbers below 100 * //
   for ($n = 1; $n < 100; $n++)
       if (is_perfect($n))
           echo $n . '<br />';        
?>
 
© 2004-17 robson | cc unless stated