Algorithms..

Can someone please tell me how to write the algorithm that determines if a number is a 'perfect number'? I know that the first four perfect numbers are 6, 28, 496, and 8128. I also know that a perfect number is a number that is equal to the sum of all of its factors plus 1. (excluding the number itself) Ex: 6 = 1 + 2 + 3

I just can't seem to figure out the algorithm to use this in a program function that would determine if a number is a 'perfect number'.

Thanx,
Tony

Comments

  • : Can someone please tell me how to write the algorithm that determines if a number is a 'perfect number'? I know that the first four perfect numbers are 6, 28, 496, and 8128. I also know that a perfect number is a number that is equal to the sum of all of its factors plus 1. (excluding the number itself) Ex: 6 = 1 + 2 + 3
    :
    : I just can't seem to figure out the algorithm to use this in a program function that would determine if a number is a 'perfect number'.
    :
    : Thanx,
    : Tony
    :

    this is a good question... explaining the answer would be rather difficult so i'd rather give you a small block of code to fiddle with. well here goes...

    double temp; //temporary number
    double number; //number entered by user
    int sumofdivisors = 1; //sum of the divisors...duh

    cout << "---Perfect Numbers---
    ";
    cout << "Enter a number : ";
    cin >> number;

    for(int i = 2; i < number; i++) //loop until 1 less than number
    {
    temp = number / i; //perform division of

    if(temp == (int)temp) //check if temp is a whole number (is it
    //a divisor?)
    {
    sumofdivisors += temp; //increase sum of divisors by temp
    //(whole number)
    }
    }

    if(sumofdivisors == number) //check if the sum of divisors is
    //equal to the number entered
    {
    if(sumofdivisors == 1) //check if number is 1
    {
    cout << "Sorry, that is not a perfect number.

    ";
    return 0; //exit function or main()
    }
    cout << number << " is a perfect number!!

    "; }
    else
    { cout << "Sorry, that is not a perfect number.

    "; }




    thats about it... any errors or other questions, just post

  • : Can someone please tell me how to write the algorithm that determines if a number is a 'perfect number'? I know that the first four perfect numbers are 6, 28, 496, and 8128. I also know that a perfect number is a number that is equal to the sum of all of its factors plus 1. (excluding the number itself) Ex: 6 = 1 + 2 + 3
    :
    : I just can't seem to figure out the algorithm to use this in a program function that would determine if a number is a 'perfect number'.
    :
    : Thanx,
    : Tony
    :
    So, you need first to find all the factors for a number, right?
    The factors are the values which which does not leave the remainder, except the divisors 1 and the value itself, so here is straight code, not optimized:
    [code][code=000000][color=21C6FF]
    bool IsPerfect (int iValue)
    {
    int iSum = 1;

    for (int iDivisor=2; iDivisor<iValue; iDivisor++) {
    if ((iValue % iDivisor) == 0) {
    // No remainder:
    iSum += iDivisor;
    }
    }
    return (iSum == iValue); // returns 'true' if they are equal
    }
    [/color][/code][/code]
    Test it, please... I didn't...
  • : : Can someone please tell me how to write the algorithm that determines if a number is a 'perfect number'? I know that the first four perfect numbers are 6, 28, 496, and 8128. I also know that a perfect number is a number that is equal to the sum of all of its factors plus 1. (excluding the number itself) Ex: 6 = 1 + 2 + 3
    : :
    : : I just can't seem to figure out the algorithm to use this in a program function that would determine if a number is a 'perfect number'.
    : :
    : : Thanx,
    : : Tony
    : :
    :
    : this is a good question... explaining the answer would be rather difficult so i'd rather give you a small block of code to fiddle with. well here goes...
    :
    : double temp; //temporary number
    : double number; //number entered by user
    : int sumofdivisors = 1; //sum of the divisors...duh
    :
    : cout << "---Perfect Numbers---
    ";
    : cout << "Enter a number : ";
    : cin >> number;
    :
    : for(int i = 2; i < number; i++) //loop until 1 less than number
    : {
    : temp = number / i; //perform division of
    :
    : if(temp == (int)temp) //check if temp is a whole number (is it
    : //a divisor?)
    : {
    : sumofdivisors += temp; //increase sum of divisors by temp
    : //(whole number)
    : }
    : }
    :
    : if(sumofdivisors == number) //check if the sum of divisors is
    : //equal to the number entered
    : {
    : if(sumofdivisors == 1) //check if number is 1
    : {
    : cout << "Sorry, that is not a perfect number.

    ";
    : return 0; //exit function or main()
    : }
    : cout << number << " is a perfect number!!

    "; }
    : else
    : { cout << "Sorry, that is not a perfect number.

    "; }
    :
    :
    :
    :
    : thats about it... any errors or other questions, just post
    :
    :
    Thank you so much. There is just one small thing I think needs changing.

    at this line: if(sumofdivisors == number) should read
    if(sumofdivisors + 1 == number) see original question above.

    Other than that, you have helped me solve my problem. Thank you again.
  • : Thank you so much. There is just one small thing I think needs changing.
    :
    : at this line: if(sumofdivisors == number) should read
    : if(sumofdivisors + 1 == number) see original question above.
    :
    : Other than that, you have helped me solve my problem. Thank you again.
    :
    that will give you 1 extra because sumofdivisors is initialed to 1 already. the line reads: int sumofdivisors = 1;
    so you shouldnt have to increase it by one.
  • : : Thank you so much. There is just one small thing I think needs changing.
    : :
    : : at this line: if(sumofdivisors == number) should read
    : : if(sumofdivisors + 1 == number) see original question above.
    : :
    : : Other than that, you have helped me solve my problem. Thank you again.
    : :
    : that will give you 1 extra because sumofdivisors is initialed to 1 already. the line reads: int sumofdivisors = 1;
    : so you shouldnt have to increase it by one.
    :
    Ahhhhhh, ok. Thanks again. I missed that!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories