A problem with my 'perfect numbers' Applet

I have written the following applet to cycle through 1000 integers and to determine whether or not each is a perfect number (ie. equal to the sum of it's divisors - such as 6 = 1 + 2 + 3).

I used the same mathematics in a previous applet in which the user could enter any number and the applet would tell the user whether or not the number is perfect. That applet worked perfectly.

For some reason, when I used the same method in the new applet, it returns an answer saying that every number after 1 is NOT a perfect number.

Does anybody have any idea what I am doing wrong?

(P.S. I hope the formatting is ok. I know I posted some code yesterday which did not observe the posting rules.)

//Coded by Eoghain on 28/5/07

//A program to check perfect numbers

//Third attempt!

//This nearly works!

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass PerfectNumberCheck2extends JApplet

{

int numberToCheck, sumOfDivisors, sumSoFar;

JTextArea outputArea;

JScrollPane scroller;

publicvoid init()

{

//set up GUI

Container container = getContentPane();

container.setLayout(new FlowLayout());

outputArea =new JTextArea(20, 30);

scroller =new JScrollPane(outputArea);

container.add(scroller);

}

publicvoid start()

{

//Cycle through every integer from 1 to 1000

//and check to see if each is a perfect number

for (int i = 0; i <= 1000; i++)

{

numberToCheck = i;

numberCheck(numberToCheck);

}

}

publicvoid numberCheck(int numberToCheck)

{

sumSoFar = 0;

for (int i = 1; i <= numberToCheck; i++)

{

//If the number is divisible by i, without remainder, add to sumSoFar

if (numberToCheck % i == 0)

sumSoFar += i;

}

sumOfDivisors = sumSoFar;

//Check to see if the sum of the divisors is equal to the number itself

checkPerfect(sumOfDivisors);

}

publicvoid checkPerfect(int x)

{

if (numberToCheck == x)

outputArea.append(numberToCheck +" is a perfect number.\n");

else

outputArea.append(numberToCheck +" is not a perfect number.\n");

}

}// end class

Thanks in advance,

Eoghain

[3972 byte] By [Eoghaina] at [2007-11-27 5:42:02]
# 1

You're almost there.

this:

for (int i = 1; i <= numberToCheck; i++)

should be this:

for (int i = 1; i < numberToCheck; i++)

A little thought, and it will be obvious why.

ps: your formating is beautiful. Looks like you have a great future in Java... good luck!

petes1234a at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...
# 2

> equal to the sum of it's divisors - such as 6 = 1 + 2 + 3

But the divisors of 6 are 1, 2, 3, and 6, so the sum of them is 1+2+3+6 = 12.

There's your mistake: a number N is perfect if 2N is equal to the sum of the divisors of N. Or equivalenty, if N is equal to the sum of the divisors that are less than N.

jsalonena at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...
# 3

> > equal to the sum of it's divisors - such as 6 = 1 + 2 + 3

>

> But the divisors of 6 are 1, 2, 3, and 6, so the sum of them is 1+2+3+6 = 12.

>

> There's your mistake: a number N is perfect if 2N is equal to the sum

> of the divisors of N. Or equivalenty, if N is equal to the sum of the

> divisors that are less than N.

Always them mathematical folks spoiling all the fun; they're like the Greek:

you give them a problem, they translate it to their own language and all

of a sudden you don't understand your own problem anymore ;-)

kind regards,

Jos

JosAHa at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...
# 4
> ps: your formating is beautiful. Looks like you have> a great future in Java... good luck!Right, so you haven't taken a look at the java coding conventions in a while.
macrules2a at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...
# 5

> Right, so you haven't taken a look at the java coding

> conventions in a while.

Sorry, I wasn't specific. He was specific however and was refering to posting rules not coding conventions. It is so nice (and rare) to be able to at least read the code from a newbie poster. With time and patience we can walk him through the conventions.

petes1234a at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...
# 6

> > Right, so you haven't taken a look at the java

> coding

> > conventions in a while.

>

> Sorry, I wasn't specific. He was specific however

> and was refering to posting rules not coding

> conventions. It is so nice (and rare) to be able to

> at least read the code from a newbie poster. With

> time and patience we can walk him through the

> conventions.

Ah :)

macrules2a at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks for the help!
Eoghaina at 2007-7-12 15:20:11 > top of Java-index,Java Essentials,New To Java...