Thread: [C++] Perfect Number Identifier

Page 1 of 3 123 LastLast
Results 1 to 10 of 25
  1. #1 [C++] Perfect Number Identifier 
    Registered Member
    TORONTO's Avatar
    Join Date
    Feb 2010
    Age
    30
    Posts
    873
    Thanks given
    348
    Thanks received
    212
    Rep Power
    92
    This was an in-class assignment which we had 15 min to do
    I CBF to explain what a perfect number is on this thread so go and read Perfect number - Wikipedia, the free encyclopedia

    Spoiler for AssignmentQuestion:

    In mathematics, a perfect number is a positive integer that 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 *******1 = 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.

    Write a program which gets a number and identifys if the number is perfect or not.

    Sample output " Your number can be classified as a perfect number!"
    "Your number cannot be classified as a perfect number."



    Spoiler for Answer (Code):

    Code:
    #include <iostream>
    /*
     *NotTellingRuneServerFagsMyRealName
     *This program checks to see if there is a perfect number
     *It Types out a message accordingly
     *February 25th 2010
     *ICS3U
     */
    using namespace std;
    
    /******************************************************/
    //Boolean to check if it is really a Perfect Number
    bool isPerfectNumber(int value){
                             //Get the value from the user
        cout << endl;
        cout << "The factors are" << endl;
        int totalValue = 0;  //Init value declaration
        for(int x = 1; x <= value/2; x++) { 
                //The reason its divide by 2 is because the lowest factor is 2 :)
                //For Loop Continues
        
                //Get Remainder value
            if(value%x == 0){//If the remainder of the mod division ==0
                       cout << x << endl; //Display the factors
            //Divide value by for loop cycles until it equal (Get the Factors)
                totalValue+=x;
            //Add the factors to the sum (Total Value)
            }
        }
        //If the total value of the added factors equals the perfect number
        if(totalValue==value)
            return 1;
            //Return True
        else
            return 0;
            //Return False
    }
    /******************************************************/
    
    int main(){
    
    int potentialPerfectNumber; //The value the user will enter
    int amountOfTries = 0;      //Amount of times they can enter
    
    while(amountOfTries == 0){  //As long as its 0, continue the loop
    
    potentialPerfectNumber = 0; //Set init value for the number entered  
                                //Resets every time                  
    cout << "Please enter a Value to check for perfect Numbers" << endl;
    
    cin >> potentialPerfectNumber;//Get the value    
    cout << endl;
    if(isPerfectNumber(potentialPerfectNumber)){ 
    //Check the boolean and get return value
    cout << endl;
    cout << "The number " << potentialPerfectNumber << " Is a perfect Number!" <<
    endl; //Return the true statement in a sentence
    }else{ //If it doesn't turn out true
    cout << "The number " << potentialPerfectNumber << 
    " Is NOT a perfect Number!" << endl;
    //Say its not true
    }
                        
    } 
        
    int stop; //Stopper, really not needed but whatever :p
    cin >> stop;
    }
    retired from RSPS for good

    EDIT:
    I came back one last time just to thank this post:
    [SPOIL]
    [/SPOIL]
    Reply With Quote  
     

  2. #2  
    Renown Programmer

    Sean's Avatar
    Join Date
    May 2007
    Age
    32
    Posts
    2,757
    Thanks given
    264
    Thanks received
    1,090
    Rep Power
    4393
    that code is ugly
    Reply With Quote  
     


  3. #3  
    Registered Member
    TORONTO's Avatar
    Join Date
    Feb 2010
    Age
    30
    Posts
    873
    Thanks given
    348
    Thanks received
    212
    Rep Power
    92
    LOL chill its C++ and im in my second year at high school, i'll learn slowly dw
    Also my teacher said if we don't have comments we lose 30% of our mark, so i wrote bullshit.
    retired from RSPS for good

    EDIT:
    I came back one last time just to thank this post:
    [SPOIL]
    [/SPOIL]
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Feb 2011
    Posts
    115
    Thanks given
    0
    Thanks received
    76
    Rep Power
    158
    Quote Originally Posted by iLankan View Post
    LOL chill its C++ and im in my second year at high school,
    spoiler: you learnt nothing.
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,147
    Thanks given
    2,656
    Thanks received
    3,731
    Rep Power
    5000
    I know this is the tutorial section but I think it'd be nice to give some feedback about that code.

    Your indentation is a bit inconsistent in places. This may well be because of the forum and perhaps you are using a text editor or IDE that formats things in a different way, but the whole main() function needs some indentation and the isPerfectNumber() function needs to be more consistent - the comments being more indented than the code itself makes them much harder to read.

    Some variable names like "potentialPerfectNumber" and "totalValue" are quite long. I'd just simply name them "num" and "total" or "sum".

    In many ways you've commented too much.

    Comments, in my opinion, should add something to the code rather than state what the code means a second time. For example:

    Code:
    // declares the sum variable and sets it to zero
    int sum = 0;
    is a useless comment whereas this:

    Code:
    // stores the sum of all the factors
    int sum = 0;
    actually adds to the code rather than repeating it in human form.

    If you took all/some of this advice, your isPerfectNumber method would look more like this which in my opinion is much more elegant even though none of the logic changed:

    Code:
    // A function to check if the specified number is perfect. A perfect number is
    // a positive integer that is the sum of all of its factors.
    bool is_perfect(int num)
    {
      // If the number is zero or negative it is not perfect.
      if (num <= 0)
        return false;
    
      // Stores the sum of all the factors.
      int sum = 0;
    
      // Loops through all the factors of the number and adds them to the sum.
      for (int factor = 1; factor <= (num / 2); factor++)
        if (num % factor == 0)
          sum += factor;
    
      // If the number equals the sum of its factors it is perfect.
      return num == sum;
    }
    .
    Reply With Quote  
     

  7. #6  
    Registered Member
    TORONTO's Avatar
    Join Date
    Feb 2010
    Age
    30
    Posts
    873
    Thanks given
    348
    Thanks received
    212
    Rep Power
    92
    Quote Originally Posted by Graham View Post
    I know this is the tutorial section but I think it'd be nice to give some feedback about that code.

    Your indentation is a bit inconsistent in places. This may well be because of the forum and perhaps you are using a text editor or IDE that formats things in a different way, but the whole main() function needs some indentation and the isPerfectNumber() function needs to be more consistent - the comments being more indented than the code itself makes them much harder to read.

    Some variable names like "potentialPerfectNumber" and "totalValue" are quite long. I'd just simply name them "num" and "total" or "sum".

    In many ways you've commented too much.

    Comments, in my opinion, should add something to the code rather than state what the code means a second time. For example:

    Code:
    // declares the sum variable and sets it to zero
    int sum = 0;
    is a useless comment whereas this:

    Code:
    // stores the sum of all the factors
    int sum = 0;
    actually adds to the code rather than repeating it in human form.

    If you took all/some of this advice, your isPerfectNumber method would look more like this which in my opinion is much more elegant even though none of the logic changed:

    Code:
    // A function to check if the specified number is perfect. A perfect number is
    // a positive integer that is the sum of all of its factors.
    bool is_perfect(int num)
    {
      // If the number is zero or negative it is not perfect.
      if (num <= 0)
        return false;
    
      // Stores the sum of all the factors.
      int sum = 0;
    
      // Loops through all the factors of the number and adds them to the sum.
      for (int factor = 1; factor <= (num / 2); factor++)
        if (num % factor == 0)
          sum += factor;
    
      // If the number equals the sum of its factors it is perfect.
      return num == sum;
    }
    The problem is you don't know how much Comments the teacher is expecting, each one has their own way and it gets annoying
    retired from RSPS for good

    EDIT:
    I came back one last time just to thank this post:
    [SPOIL]
    [/SPOIL]
    Reply With Quote  
     

  8. #7  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,147
    Thanks given
    2,656
    Thanks received
    3,731
    Rep Power
    5000
    Quote Originally Posted by iLankan View Post
    The problem is you don't know how much Comments the teacher is expecting, each one has their own way and it gets annoying
    Yeah, the teacher for the course I do isn't amazing. They think C++ is actually called CC+ .
    .
    Reply With Quote  
     


  9. #8  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,480
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    last i checked numbers that can only be divided by one and itself is a PRIME number (and is not 1)
    Reply With Quote  
     

  10. #9  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,480
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    Code:
    int isPrime(int n)
    {
      if (n < 2)
        return 0;
    
      if (n == 2)
        return 1;
    
      if ((n & 1) == 0)
        return 0;
    
      int range = (int) sqrt((double) n);
      for (int i = 3; i < range; i += 2)
        if (n % i == 0)
          return 0;
    
    
      return 1;
    }
    Reply With Quote  
     

  11. #10  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,147
    Thanks given
    2,656
    Thanks received
    3,731
    Rep Power
    5000
    Quote Originally Posted by Killer 99 View Post
    last i checked numbers that can only be divided by one and itself is a PRIME number (and is not 1)
    Read the thread again...

    Quote Originally Posted by iLankan View Post
    In mathematics, a perfect number is a positive integer that is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself.
    .
    Reply With Quote  
     

  12. Thankful user:


Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. <identifier> expected
    By PrataKosong in forum Help
    Replies: 10
    Last Post: 11-18-2010, 11:04 AM
  2. [MEMBER] System (Identifier)
    By nl pk3er in forum Help
    Replies: 27
    Last Post: 06-27-2010, 07:36 PM
  3. Identifier Required. (please help)
    By Bars in forum Help
    Replies: 4
    Last Post: 07-21-2009, 12:14 AM
  4. ERROR: identifier expected HELP REP++
    By Ophion in forum Help
    Replies: 7
    Last Post: 05-06-2009, 10:28 PM
  5. Identifier error
    By D I M E in forum Help
    Replies: 0
    Last Post: 10-28-2008, 03:26 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •