How to print the one thousandth abundant odd number in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

// abundant odd number = sum of proper divisors > number
 
unsigned SumOddNumberProperDivisors(const unsigned num) {
    unsigned sum = 1;
 
    for (unsigned i = 3, j; i < sqrt(num) + 1; i += 2)
        if (num % i == 0)
            sum += i + (i == (j = num / i) ? 0 : j);
 
    return sum;
}
 
int main()
{
    unsigned num = 1;
 
    for (unsigned i = 0; i < 1000; num += 2) {
        if (SumOddNumberProperDivisors(num) > num) {
            i++;
        }
    }
 
    std::cout << num;
}
 
 
 
 
/*
run:
 
492977
 
*/

 



answered Oct 23, 2022 by avibootz

Related questions

1 answer 101 views
1 answer 107 views
1 answer 125 views
...