To find if a number is Perfect, really fast:
For values of n starting at 2 and going up, try:
(2^n - 1) This will give you the value of 3.
You can check if a number is prime with this function I wrote:
Code:
bool checkIfPrime(int num){
int factor = num / 2; //begin from halfway, work way down to 1 (since 2 * (num / 2) = (num / 2) * 2)
while (factor > 1){ //until you are greater than 1. 1 is not prime; if you get this far, num is prime
if (num % factor == 0) //something other than 1 and num fit into num
return(false); //return false, num is NOT prime
factor--; //check next lowest value to see if it fits into num
}
return(true); //left loop after factor equaled 1, num is prime, return true
} //end bool checkIfPrime
If the number is not prime, increase n and try the (2^n - 1) again and recheck if it's prime.
IF IT IS PRIME... which 3 is... multiply the answer you got from that first equation (3) by (2 ^ (n - 1)). (2 ^ (n - 1)) will equal 2. 2 times 3 is... 6. A perfect number.
__________________________________________________ _
Algorithm:
1. Start variable n at 2.
2. Get the result from 2^n - 1 (code for this is (pow(2, n) - 1).
3. The RESULT of this equation is checked if it's prime using the function above. For this, just make a bool variable like IsPrime and call isPrime = checkIfPrime(num);
4. If the number is not prime, increase n and go back to step two.
5. If the number is prime, multiply (2^n - 1) by (2 ^ (n - 1)). The code looks like (pow(2, n) - 1) * (pow(2, n - 1)). This will result in a perfect number.
6. Repeat these steps as much as you want using a loop.
7. Note of warning: These numbers get really big, really fast. You should also check for overflow error *just check if the perfect number you get is negative. If it is, output "OVERFLOW ERROR"*
Hope this helps
__________________________________________________ __