Finding an odd perfect number
- by Coin Bird
I wrote these two methods to determine if a number is perfect. My prof wants me to combine them to find out if there is an odd perfect number. I know there isn't one(that is known), but I need to actually write the code to prove that.
The issue is with my main method. I tested the two test methods. I tried debugging and it gets stuck on the number 5, though I can't figure out why. Here is my code:
public class Lab6
{
public static void main (String[]args)
{
int testNum = 3;
while (testNum != sum_of_divisors(testNum) && testNum%2 != 0)
testNum++;
}
public static int sum_of_divisors(int numDiv)
{
int count = 1;
int totalDivisors = 0;
while (count < numDiv)
if (numDiv%count == 0)
{
totalDivisors = totalDivisors + count;
count++;
}
else
count++;
return totalDivisors;
}
public static boolean is_perfect(int numPerfect)
{
int count = 1;
int totalPerfect = 0;
while (totalPerfect < numPerfect)
{
totalPerfect = totalPerfect + count;
count++;
}
if (numPerfect == totalPerfect)
return true;
else
return false;
}
}