Perfect numbers | Rosetta Code | #8

URL to the problem page: http://rosettacode.org/wiki/Perfect_numbers


A perfect number is a positive integer that is the sum of its proper 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).

It is not known if there are any odd perfect numbers (any that exist are larger than 102000).
The number of known perfect numbers is 50 (as of September, 2018), and the largest known perfect number contains over 46 million decimal digits.



#include <iostream>
using namespace std;

int main()
{
    int sum, result = 0;

    cout << "First 4 perfect numbers are:" << endl;
    for (int i = 1; result <= 3; i++) {
        sum = 0;
        for (int j = 1; j <= (i / 2); j++) {
            if (i % j == 0) {
                sum += j;
            }
            if (sum > i) {
                break;
            }
        }
        if (sum == i) {
            result++;
            cout << i << endl;
        }
    }
    return 0;
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac