C# perfect numbers exercise

cmathperfect-numbers

can you help me with the following exercise pls? (it's not homework, just an exercise in the book I'm using.)

"An integer is said to be a perfect number if its factors, including one (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write method Perfect that determines whether parameter value is a perfect number. Use this method in an app that determines and displays all the perfect numbers between 2 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect."

so here's what i got so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Perfect_Numbers2
{
    class Program
    {
        static bool IsItPerfect(int value)
        {
            int x = 0;

            int counter = 0;

            bool IsPerfect = false;

            List<int> myList = new List<int>();

            for (int i = value; i <= value; i++)
            {
                for (int j = 1; j < value; j++)
                {
                    // if the remainder of i divided by j is zero, then j is a factor of i
                    if (i%j == 0) {
                        myList[counter] = j; //add j to the list
                        counter++;
                    }
                    for (int k = 0; k < counter; k++)
                    {
                        // add all the numbers in the list together, then
                        x = myList[k] + myList[k + 1]; 
                    }
                    // test if the sum of the factors equals the number itself (in which case it is a perfect number)
                    if (x == i) {
                        IsPerfect = true;
                    }
                }
                Console.WriteLine(i);
            }
            return IsPerfect;
        }
        static void Main(string[] args)
        {
            bool IsItAPerfectNum = false;

            for (int i = 2; i < 1001; i++)
            {
                IsItAPerfectNum = IsItPerfect(i);
            }
        }
    }
}

how would you do it? is my code fixable? how would you fix it? thanks!

im getting an error at line myList[counter] = j; (index was out of range) and besides it's not displaying the perfect numbers like it's supposed to….

EDIT = I made some changes;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Perfect_Numbers2
{
class Program
{
    static bool IsItPerfect(int value)
    {
        int x = 0;

        int counter = 0;

        bool IsPerfect = false;

        List<int> myList = new List<int>();

        for (int i = value; i <= value; i++)
        {
            for (int j = 1; j < i; j++)
            {
                if (i%j == 0)  // if the remainder of i divided by j is zero, then j is           a factor of i
                {
                    myList.Add(j); //add j to the list


                }
                x = myList.Sum();
                if (x == i)                        // test if the sum of the factors       equals the number itself (in which case it is a perfect number)
                {
                    IsPerfect = true;
                }

        }
            Console.WriteLine(i);
        }
        return IsPerfect;
    }
    static void Main(string[] args)
    {
        bool IsItAPerfectNum = false;

        for (int i = 2; i < 1001; i++)
        {
            IsItAPerfectNum = IsItPerfect(i);
            Console.WriteLine(IsItAPerfectNum);
            Console.ReadKey(true);
        }
    }
  }
  }

now i can cycle through all the numbers until 1000 and it displays if it's perfect or not (true or false) [which isn't what the exercise called for, but it's a step in the right direction (the exercise says that it should display only the perfect numbers)].

In any case, what's strange is that it says true at number 24, which isn't a perfect number…. http://en.wikipedia.org/wiki/Perfect_numbers#Examples

why is 24 different?

thanks very much

Best Answer

can you help me with the following exercise please?

Yes. Rather than showing you where your error is, I'll teach you how to find your error. Even better, the same technique will lower the chances of you causing the error in the first place.

The key here is to break the problem down into small parts where each small part can be tested independently. You have already started to do this! You have two methods: Main and IsItPerfect. You should have at least three more methods. The methods you should have are:

  • IsDivisor -- takes two integers, returns true if the first divides the second.
  • GetAllDivisors -- takes an integer, returns a list of all the divisors
  • Sum -- takes a list of integers, returns the sum

Your method IsPerfect should be calling GetAllDivisors and Sum and comparing the sum to the original number, and that's all it should be doing. Your method GetAllDivisors should be calling IsDivisor, and so on.

You can't find the bug easily because your method is doing too much. If you're not getting the correct result out and you have four methods instead of one then you can test each method independently to make sure that it works, or fix it if it does not.