Perfect number

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;
 }
 
 

www.EuAutodily.cz SEO od společnosti Digital Pylon


Online casino s algoritmem

České casino online online slot-vegas.cz





Zajímavé články: Jak najít práci snů? Zvolte kariéru v IT!, Češi mají rádi hrací automaty online, Jak funguje algoritmické obchodování Casino, Online výuka Algoritmus a online marketing mají svá pravidla, Automaty, Matematický vliv, Ratings, Jak fungují algoritmy hazardních her online: více znalostí, více peněz, SYPWAI - nástroj pro vědecký vývoj, Vynikají na globálním trhu: Nejlepší vývojáři softwaru pro online výherní automaty, Jak si vybrat nejlepší české online casino, Proč byste měli hrát online casino VPN revoluce, Kde najdeme algoritmy v každodenním životě?