Hello, I am working on a java program that finds perfect numbers. I need to make it run more efficiently by touching up the computation I have in my factorSum method. Heres what I have so far:

``````public static int factorSum(int testNum)
{
//Initialize local variable accumulator to zero.
int accumulator = 0;

for (int counter = 2; counter < Math.sqrt(testNum); counter++)
{

if (testNum % counter == 0 && counter != testNum)
{
accumulator = (counter + (testNum / counter));

}//end if statement

}//end for loop

//return accumulator to isFactor method
return accumulator;

}//end factorSum method``````

It isnt outputting the perfect numbers. Can someone guide me in the right direction to get this computational code correct?

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by brandongood

What are perfect numbers?

In mathematics, a perfect number is defined as a positive integer which is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself), or σ(n) = 2n.

The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6.

The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128

I figured it out! Thanks to all that took a look at my question!