确定数的因数 当除数之和不大于该数时,程序将不接受大于 10000 的限制

Determining factors of numbers when sum of divisors are not larger than the number, program will not accept a limit larger than 10000

本文关键字:程序 不接受 10000 大于 不大于      更新时间:2023-10-16

当我将限制从 100 更改为 1,000,000 时,它不会打印出所有数字,它会停在 5 位数字处,并且不会计算得更高。这是为什么呢?

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) 
{
const int limit = argc > 1 ? atoi(argv[1]) : 100;
const int badness = argc > 2 ? atoi(argv[2]) : 1;

for (int num = 2; num < limit; num++) {
    int total = 1;
    for (int factor = 2; factor * factor <= num; factor++) {
        if (num % factor == 0) {
            total += factor;

            if (factor * factor != num) {
                total += (num / factor);
            }
        }

        }
        if (abs(num - total) <= abs(badness)) {
            cout << num << " " << flush;
        }
    }
}

看看这个 https://en.wikipedia.org/wiki/C_data_types

这可能是因为您使用的是 int。

尝试使用长整型或无符号长整型