繁体   English   中英

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

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

当我将限制从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。

尝试使用long而不是unsigned long

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM