2

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 != 2)
     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;
}
}
false
  • 10,264
  • 13
  • 101
  • 209
coinbird
  • 1,202
  • 4
  • 24
  • 44
  • When you say "debugging", do you mean actually using a debugger? A debugger should show you which method the program is getting stuck in. – chrylis -cautiouslyoptimistic- Oct 30 '13 at 03:45
  • 1
    you aren't using is_perfect in this code – Sean F Oct 30 '13 at 03:46
  • `testNum%2` is `0` or `1`, never `2`. – Teepeemm Oct 30 '13 at 03:50
  • I know. I couldn't find a way to fit it in the test I want to do. – coinbird Oct 30 '13 at 03:51
  • That should work (though is_perfect isn't used). Try including Logcat calls in your code to see what values you have. Also, the modulus not equalling 2 should make this a constant loop, no? – zgc7009 Oct 30 '13 at 03:56
  • It is probably better to roll back the [edit](http://stackoverflow.com/posts/19673433/revisions) to the original that breaks, so the answer makes sense.. ;) – Andrew Thompson Oct 30 '13 at 03:56
  • BTW - please *stop* adding JAVA at the end of the titles of questions. It is spelled Java and it is unnecessary to add the major tag in the title anyway. – Andrew Thompson Oct 30 '13 at 03:59
  • 1
    @AndrewThompson - You'd have done the rollback when you saw that edit, than asking the OP :) Have a look at [this question](http://meta.stackexchange.com/q/201874/216721) I recently asked on Meta, where the solution suggestion to such an edit is, indeed, a rollback. – Rahul Oct 30 '13 at 04:03

2 Answers2

3

Make

testNum%2 != 2

as

testNum%2 != 0
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • How does this fix it if you don't mind me asking. Something modulus 2 never equals 2 so how does that make this stop at 5 and not stack overflow? – zgc7009 Oct 30 '13 at 03:59
  • @zgc7009, I'm not sure why @CoinBird said it stopped at `5`. I believed it actually stopped at `6` (the first perfect number, though not odd). – Paul Draper Oct 30 '13 at 04:04
0
testNum=3
while (testNum != sum_of_divisors(testNum) && testNum%2 != 2)
    testNum++;

You may want to do 'testNum+=2' since you are concerned about only odd numbers and replace the testNum %2!=2 with testNum>0 or other stopping condition. Eventually your integers will overflow.

"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."

Do you mean between 3 & 2^32-1? It is not known that there are no odd perfect numbers.

Lan
  • 1,206
  • 1
  • 11
  • 26