I'm going to bite on this one for a couple of reasons....
I had to do this project about 20 years ago in Pascal and I was wondering how it would translate to the .NET world. I like doing stuff like this because it forces me to learn new things. And one good example of this is that I never knew that you were able to sum a List. Pretty neat .NET feature. In the "old" days we would have had to create an array and then loop through it adding each element as we went... uphill.. both ways... in the snow.
I'm going to consider this a learning experience, heck, I learned something today. So here's my advice: Always take your problem and split it up into the smallest components possible. Yes, think about the big problem, but break it down.
Think about it, what is the smallest component of this? You know you are going to have to do two basic things:
#1. Check to see if a number divides evenly.
#2. Check to see if all of the divisors equal the number that you are testing.
Sounds like you have two discrete methods if you ask me.
So let's see if the number divides evenly:
public bool TestForEvenlyDivisable(int iNumerator, int iDenominator)
{
if ((iNumerator % iDenominator) == 0)
{
return true;
}
else
{
return false;
}
}
The little % you see is the mod operator. You need this to test if a number is evenly dividable.
Now let's look at the other problem. You have to check to see if the numbers that divided successfully actually add up to the number you are testing. In the old days, I would have passed an array, or added onto an integer. Now, we have generics and collections. It's progress grand?
public bool TestForSumOfDivisors(int iNumberToTest, List<int> Divisors)
{
if (iNumberToTest == Divisors.Sum())
{
return true;
}
else
{
return false;
}
}
That little
Divisors.Sum()
saves a lot of time.
So now how do we put it all together?
Well, we are going to be dealing with FOR loops. And specifically a loop inside of a loop. You need to loop from 2 (because we know a perfect number can't be 1) all the way to N, which in your case would be 1000. Inside that loop, we have to loop from 1 all the way up to the number that we are currently testing.
Sorry, there is no shortcut. This is old school
Colossus-style brute force assault.
In the first loop, you get the number that you are testing. In the second loop, you check to see if each number is evenly dividable. If it is, great, add it to the List. When you are finished with that test, then test to see if the divisors sum to the number you are currently testing.
for (int i = 2; i <= nThNumber; i++)
{
List<int> ListOfDivisableNumbers = new List<int>();
for (int j = 1; j < i; j++)
{
if (TestForEvenlyDivisable(i, j) == true)
{
ListOfDivisableNumbers.Add(j);
}
}
if (TestForSumOfDivisors(i, ListOfDivisableNumbers) == true)
{
m_sResults += i.ToString() + "...";
}
}
Now I've given you all of the tools you need to do this. You can figure out the rest on how to throw all of this stuff into a class and hook it up so it works.
By the way, there is a way to make this program run twice as fast. I'll leave coding that up to you, but here is a hint: you don't need to test numbers that are greater than half of the test case.
Good luck my friend. There is a rewarding career out there if you buckle down and stick with it.
Ryan McBeth