C# Perfect Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wkid87
    New Member
    • Oct 2007
    • 13

    C# Perfect Numbers

    Using C# Microsft Visual C#, Console Application

    I'm needing help with the user entering in a number to tell if it perfect or not. I have the perfect number figure out. Here in the instruction:

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

    Your solution shoud be the followings:

    Write a class called PerfectNumber with two methods. The first method:
    public string Perfect( int value ) that determines if value is perfect and if so returns a string that contains the factors.

    Hint on how to have a string that contains the factors:
    string factors = “1“;
    factor += “ + “ + fac; // fac is a factor of the perfect number

    The second method:
    public void FindPerfects()
    that determintes the perfect numbers between 2 and 1000 and prints their factors.

    The Main method simply creates an object of PerferNumber and calls
    FindPerfects().
    You must compile and test the program.
    Last edited by wkid87; Nov 4 '07, 05:49 PM. Reason: code didn't post
  • wkid87
    New Member
    • Oct 2007
    • 13

    #2
    Here is what I have so far:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class PerfectNumbers
        {
            static void Main(string[] args)
            {
    
            }
            public string Perfect(int value)
            {
    
            }
    
            public void FindPerfects()
            {
                int sum = 0, x = 0;
                for (int num = 1; num < 1000; num++)
                {
                    for (int factor = 1; factor < num; factor++)
                    {
                        x = num % factor;
                        if (x == 0)
                            sum = sum + factor;
                    }
                    if (sum == num)
                    {
                        Console.WriteLine("\n\n" + num + ":");
                        Console.WriteLine("\nThe factors are: ");
    
    
                        for (int factor = 1; factor < num; factor++)
                        {
                            x = num % factor;
                            if (x == 0)
                                Console.WriteLine(factor);
                        }
                    }
                    sum = 0;
                }
            }
        }
    }

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      a). We're not supposed to help with assignments

      b). You could improve some efficiencies by only running your loop from 1 through the square root of your number...
      i). Let n = Number
      ii). Let a = Factor 1 (which can be any number in the domain of possible factors [1 ~ Sq. root of n])
      iii). Let b = n / a (a variation of this rule would be used for increased numbers of factors).

      Iterating through possible A's will effectively give you both factors by using rule iii so you don't actually need to iterate all the possible factors to actually calculate all the factors.

      c). Hint: Look up Euclid's theorem: n is perfect if n = [2^(m-1)](2^m - 1)

      Comment

      Working...