Perfect number...

This is a discussion on Perfect number... within the C++ Programming forums, part of the General Programming Boards category; Does anyone knows why this isn't working? I got a problem where I must make a program in C++ which ...

  1. #1
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21

    Perfect number...

    Does anyone knows why this isn't working?
    I got a problem where I must make a program in C++ which finds the percet

    number between 2 and number m(which user enters).
    Anyway,I made the program to see if the number you entered is perfect number

    but I can't make it so that it calculates which number between 2 and m is the

    perfect.
    Sorry my english is not so good.
    Note:The perfect number is number which is equal to the sum of his (dividive

    numbers?)
    I'm sorry I don't know how to write it in english,but here is example:
    28=1+2+4+7+14
    Number 28 can be divided by number 2,4,7,14,28.Sum of that dividing is 28.
    Here's the number which checks is the entered number(only one

    number)perfect:
    *********************
    Code:
    #include<iostream.h>
    void main(){
    int x,suma=0,number;
    cout<<"Please insert number which will be checked if it is a perfect\n"; 
    
    cout<<"number:\n";
    cin>>broj;
    for(int i=2;i<=number;i++){
        if(broj%i==0){
           suma=suma+(number/i);
           }
        }
    if(suma==number)cout<<number;
    cin>>x;                                             //so that the program doesn't close
    return;
    }
    ********************************
    And here is the program that isn't working somehow...:
    *******************************
    Code:
    #include<iostream.h>
    void main(){
    int m,x,suma=0;
    cout<<"Please enter number m:\n";
    cin>>m;
    for(int i=2;i<=m;i++){
        for(int j=2;j<=i;j++){
           if(i%j==0)
              suma=suma+(i/j);
           }
        if(suma==i)
           cout<<i<<endl;
       }
    cin>>x;                                               //so that the program doesn't close
    return;
    }
    ***********************************
    What do you think?
    Please tell me,I'm very interested in this problem,and I've been smashing my

    head for so long...
    Last edited by Argo_Jeude; 07-11-2005 at 03:31 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You must reset suma to 0 each time inside your first loop.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    #include <iostream.h>
    Should use this one if possible:

    Code:
    #include <iostream>
    using namespace std;
    Code:
    void main()
    {
        ...
    }
    The main function should always return an int:

    Code:
    int main()
    {
        ...
    
        return 0;
    }
    Code:
    int x;
    
    ...
    
    cin>>x;       //so that the program doesn't close
    Usually you would just do this:

    Code:
    cin.get();  // So that program does not close
    That way you don't need an extra junk/garbage variable.

    You should make a function out of your code that determines if a number is perfect or not. Have it return a bool (true/false) value. Also, your code does not look correct based on your description of what a perfect number is, here is some revised code:
    Code:
    bool IsPerfectNumber(int value)
    {
        int sum = 0;
    
        for( int i = 1; i <= value/2; i++ )
        {
            if( value % i == 0 )
                sum += i;
        }
    
        return sum == value;
    
    }
    Then you can call that function repeatedly inside of a loop check the numbers from 2 to m.

    Code:
    int m;
    cout<<"Please enter number m:\n";
    cin>>m;
    for(int i=2;i<=m;i++)
    {
        if( IsPerfectNumber(i) )
            cout << i << endl;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    ... and main always returns an int.
    *edit* A point to which I was beaten...

    And some info which may help you with your algorithm: http://www.answers.com/topic/perfect-number

    In particular, the formula using Mersenne primes, the endings in 6 and 8, and no odd perfect numbers below an insanely large threshold.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    do not void main itll give you an error. just to tell you also your mixing C and C++ together.

    \n works but its not the best way thats the C way

    ex.

    Code:
    cout << "text\n";
    
    // is the same as this in C++
    
    cout << "text" << endl; //then endl is the same as \n
    try reading thesse tutorials explicitly for C++ there great even a little better than this sites *SHH* for C++ that is but these forums are great!

    http://cplus.about.com
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There is one subtle difference between '\n' and endl (which surprisingly few books/tutorials seem to mention): endl inserts a new line, and flushes the buffer (which you could do explicitly). But, since you should flush the buffer, at least at the end of your outputting of stuff, endl is a very good way to do that.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Code:
    bool IsPerfectNumber(int value)
    {
        int sum = 0;
    
        for( int i = 1; i <= value/2; i++ )
        {
            if( value % i == 0 )
                sum += i;
        }
    
        return sum == value;
    
    }
    This algorithm could be made more efficient. Instead of looping up to n/2, it could loop up to the square root of n.

    (We could get way more efficient than this, but why bother?)

    Code:
    bool IsPerfectNumber(int value)
    {
        int sum = 1;
    
        int root = (int) sqrt((double) value);
        int sq = root * root;
        if (sq == value) {
            return false;
        }
    
        ++ root;
        sq += 2 * root + 1;
        /* sq now equals (root + 1) * (root + 1) */
    
        /* I'm no expert on floating point errors, so I double check. */
        if (sq == value)
            return false;
    
        /* Start at 2 because sum starts at 1. */
        for( int i = 2; i < root; ++i )
        {
            if( value % i == 0 ) {
                sum += i;
                sum += value / i;
            }
        }
    
        return sum == value;
    
    }
    Note that the perfect squares get ruled out right away because otherwise their square root would get double-counted in the sum, and that might be able to cause a false positive. (It might not be able to cause a false positive, but we don't know that.) No perfect square can be a perfect number anyway. This is provable. (The sum of the factors of an even perfect square is always odd; the sum of the factors of an odd perfect square is always even.)

    (If you did not know this (and it really isn't healthy to know these things) you could still just make sure that in the case of a perfect square, you don't add the square root twice.)
    Last edited by Rashakil Fol; 07-11-2005 at 07:11 PM.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    9
    If I may, I spent quite some time trying to do the same thing. The key to finding perfect numbers efficiently is to first find Mersenne primes. Every mersenne primes correlates to exactly one perfect number.

  9. #9
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    Wow!
    Thanks everyone,you really helped me.Oh man,how could I forget to set the suma to 0 again!!??
    And all this info about the thnigs I put and it could be better,thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect number and divisors
    By Lissa in forum C Programming
    Replies: 31
    Last Post: 10-24-2008, 01:36 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  5. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 03:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21