A natural number n is perfect, if it's value is equal to a sum of all his positive divisors (excluding number n itself). The first perfect numbers are 6, 28, 496, 8128 and all of them are in form 2^{n-1} \\cdot (2^{n}-1). Greek mathematician Euclid proved that the formula is en even perfect number whenever 2^{p-1} is a prime.

So far only even perfect numbers have been discovered, but the existence of odd perfect numbers was never disproved. According to the current state of knowledge, the odd perfect number must be higher than 10^{300}.

Examples

6 = 3 + 2 + 1
28 = 14 + 7 + 4 + 2 + 1
496 = 248 + 124 + 62 + 31 + 16 + 8 + 4 + 2+ 1
8128 = 4064 + 2032 + 1016 + 508 + 254 + 127 + 64 + 32 + 16 + 8 + 4 + 2 + 1

Code

     /**
      * Checks whether the number is perfect
      * @param number tested number
      * @return true if the number is perfect, false otherwise
      */
     public static boolean isPerfect(long number) {
         //odd perfect number (probably) does not exist, and even if it does, it has a higher value than long can represent
         if (number % 2 == 1) {
             return false;
         }
 
         long result = 1; //1 is a trivial divisor
         long i = 2;
         while (i * i <= number) { //until i <= sqrt(number)
             if (number % i == 0) {
                 result += i;
                 result += number / i;
             }
             i++;
         }
         if (i * i == number) { //perfect square, sqrt(number) was added twice
             result -= i;
         }
         return result == number;
     }
 
 /**
  * Checks whether the number is perfect
  * @param $number tested number
  * @return true if the $number is perfect, false otherwise
  */
 function isPerfect($number) {
     if ($number % 2 == 1) {
         return false;
     }
 
     $result = 1; //1 is a trivial divisor
     $i = 2;
     while ($i * $i <= $number) { //until i <= sqrt(number)
         if ($number % $i == 0) {
             $result += $i;
             $result += $number / $i;
         }
         $i++;
     }
     if ($i * $i == $number) { //perfect square, sqrt(number) was added twice
         $result -= $i;
     }
     return $result == $number;
 }
 
 







       
 

Place for your banner

Here is the position ready for our customer's banners.