Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi all,

C# Program that outputs all perfect numbers less than 1000.

The Proper divisors of a positive integer 'n' are those integers,excluding 'n' itself,that divide 'n' exactly.
For Example : Proper divisor of 12 are 1,2,3,4 and 6.

A perfect number is the Positive Integer that is equal to Sum of its proper divisors.
For Example : 6 is a perfect number because 6 = 1 + 2 + 3 .

Can any one help on this issue ?

Thanks in advance

S.Naveen...
Posted
Updated 23-Sep-10 12:24pm
v2
Comments
Toli Cuturicu 23-Sep-10 18:25pm    
We know what a perfect number is. Don't insult us by pretending to explain it!
Yusuf 23-Sep-10 18:41pm    
Hmmm.... unable to do your own homework, huh?

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:
C#
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?

C#
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.

C#
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
 
Share this answer
 
Comments
DavidKiryazi 23-Sep-10 23:58pm    
Vote of 5:

Even though its academic, its still good to see someone explaining the concept. I guess its either the OP is lazy (in which case he will get found out sooner or later by a potential employer) or the techer/lecturer did not explain the concepts as clearly as done in this post. Hopefully, its the latter
I see you're busy spamming every forum with your homework. A more efficient use of your time would be just to use google

http://answers.yahoo.com/question/index?qid=20081028061230AAKWF6P[^]

GOOGLE GOOGLE L@@K L@@K GOOGLE GOOGLE

http://www.google.co.uk/i[^]

You type things into it and click search and it will find them for you. It's amazing.
 
Share this answer
 
Console.WriteLine(6);
Console.WriteLine(28);
Console.WriteLine(496);

Voila!
 
Share this answer
 
Comments
Toli Cuturicu 24-Sep-10 6:57am    
To the folks who downvoted me: Poor you, you just failed to catch the irony.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900