Abundant odd numbers | Rosetta Code | #13

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


An Abundant number is a number n for which the sum of divisors σ(n) > 2n,
or, equivalently, the sum of proper divisors (or aliquot sum) s(n) > n.

E.G.
12 is abundant, it has the proper divisors 1,2,3,4 & 6 which sum to 16 ( > 12 or n);
or alternately, has the sigma sum of 1,2,3,4,6 & 12 which sum to 28 ( > 24 or 2n).

Abundant numbers are common, though even abundant numbers seem to be much more common than odd abundant numbers.
To make things more interesting, this task is specifically about finding odd abundant numbers.

Find and display here: the one thousandth abundant odd number and either its proper divisor sum or sigma sum.



#include <iostream>
using namespace std;

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

    cout << "1.000th abundant odd number is  =  ";
    for (int i = 1; cnt < 1000; i += 2) {
        sum = 0;
        for (int j = 1; j <= (i / 2); j += 2) {
            if (i % j == 0) {
                sum += j;
            }
        }
        if (sum > i) {
            cnt++;
            if (cnt == 1000) {
                cout << i << endl << endl << "Its proper divisor sum is  =  " << "1";
                for (int j = 3; j <= (i / 2); j += 2) {
                    if (i % j == 0) {
                        cout << " + " << j;
                    }
                }
                cout << "  =  " << sum << endl;
                break;
            }
        }
    }
    return 0;
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac