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
https://forums.phpfreaks.com/topic/90924-java-loop-through-perfect-numbers/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.