Finding an odd perfect number

Posted by Coin Bird on Stack Overflow See other posts from Stack Overflow or by Coin Bird
Published on 2013-10-30T03:42:19Z Indexed on 2013/10/30 3:54 UTC
Read the original article Hit count: 120

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about methods