-2

So this program will print perfect numbers, but one of them, 2096128, is being printed for some reason? Would really appreciate some help figuring out what is happening! Thank you! I can't figure out why one non perfect number is finding it way into the sequence!

#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>

bool isPerfect(int n);
using namespace std;

int main() {
    long long perfect = 0;
    int first = 0;

    first = (pow(2, 2 - 1))*(pow(2, 2) - 1);
    cout << first << endl;
    for (int i = 3, j = 1; j < 5; i += 2) {
        if (isPerfect(i)) {
            perfect = (pow(2, i - 1)*(pow(2, i) - 1));
            cout << perfect << endl;
            j++;
        }
    }




    // pause and exit
    getchar();
    getchar();
    return 0;
}
bool isPerfect(int n)
{
    if (n < 2) {
        return false;
    }
    else if (n == 2) {
        return true;
    }
    else if (n % 2 == 0) {
        return false;
    }
    else {
        bool prime = true;
        for (int i = 3; i < n; i += 2) {
            if (n%i == 0) {
                prime = false;
                break;
            }
        }
        return prime;
    }
}
Catz
  • 11
  • 5
  • Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Feb 16 '18 at 20:20
  • 1
    `pow` is for floating-point numbers. For integer power-of-2, it's just `(1 << x)` – Ben Voigt Feb 16 '18 at 20:26
  • 3
    `isPerfect()` doesn't return whether the number is perfect, it returns whether the number is prime. Where are you checking for perfect numbers? – Barmar Feb 16 '18 at 20:40
  • 1
    Your code is based on incorrect math. `pow(2, i - 1)*(pow(2, i) - 1)` is a perfect number whenever `pow(2, i - 1)` is prime. But you're not checking whether `pow(2, i - 1)` is prime, you're just checking whether `i` is prime. See https://en.wikipedia.org/wiki/Perfect_number#Even_perfect_numbers – Barmar Feb 16 '18 at 20:46

1 Answers1

0

You're pretty much complicating this task.

Here's what I came up with:

#include <iostream>

using namespace std;

bool isPerfect(long long n);

int main() 
{
    int count = 5;
    long long sum = 1;
    for (int i = 3; count >= 0; i += 2)
    {
        sum += i * i * i;

        if (isPerfect(sum))
        {
            cout << sum << endl;
            count--;
        }
    }

    system("pause");
    return 0;
}
bool isPerfect(long long n)
{
    int sum = 0;

    for (int i = 1; i < n; i++)
    {
        if (n % i == 0)
            sum += i;
    }

    return sum == n;
}

It sure isn't perfect, but will do for 5 numbers. Consider that it'll be very slow for more than 5 numbers.

Glitch
  • 42
  • 9