steelmanronald06 Posted February 13, 2008 Share Posted February 13, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.