Jump to content

*Java* Loop through perfect numbers


Recommended Posts

int limit = 10000;

	// Declare variables
	int n, factor, sum;

	// Loop through for perfect numbers
	for (n = 2; n <= limit; n++) {
		sum = 0;

		for (factor = 1; factor <= n/2; factor++) {
			if (n % factor == 0) {
				sum += factor;

				if (n == sum) {
					System.out.println(n);
				}
			}
		}
	}

 

Okay, that is what i've got so far.  Now here is the output:

 

6

24

28

496

2016

8128

8190

 

Now according to this wikipedia article on perfect numbers, http://en.wikipedia.org/wiki/Perfect_number, there are numbers in my return result that are not perfct.  Pretty much 24 and 2016 don't belong...there might be others. So, I need to know what i'm doing wrong. I can't seem to find why it is also including 24 and 2016 as a perfect number, considering the fact that it is NOT a perfect number.  :-/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.