Java 如何在输出中显示大于8128的完全数

Java 如何在输出中显示大于8128的完全数,java,Java,我在尝试一些关于完美数字的代码,我写了一个代码,应该显示我要求的前n个完美数字,但它只显示前4个数字,总是 我已经试着用不同的柜台等来交换位置,但都不起作用 public static void main(String[]args){ int n; //counter long N = 6; //starting point of the perfect numbers System.out.print("Please enter n: "); //maximum val

我在尝试一些关于完美数字的代码,我写了一个代码,应该显示我要求的前n个完美数字,但它只显示前4个数字,总是

我已经试着用不同的柜台等来交换位置,但都不起作用

public static void main(String[]args){
    int n; //counter
    long N = 6; //starting point of the perfect numbers
    System.out.print("Please enter n: "); //maximum value of the counter
    n = in.nextInt();
    while (n > 0) { //first loop and control of the counter
        int sum = 0, i = 1; //initialization of the variables i need for the following loop
        while (i < N -1) { //control for the iterations, maximum value is N - 1(all the numbers except N) 
            if (N % i == 0) { //control if i is a divider
                sum = sum + i; // if i ist a divider than i add it to the variable sum
            }
            i++; // i increment
        }
        if (sum == N) { //final control, if sum is equal to N, than i print it
            System.out.print(N + " ");
            n--; //decrement of the counter, so that it shows me only the numbers i ask
        }
        N++; //increment of the number to control
    }
}
publicstaticvoidmain(字符串[]args){
int n;//计数器
long N=6;//完全数的起点
System.out.print(“请输入n:”;//计数器的最大值
n=in.nextInt();
while(n>0){//第一个循环与计数器的控制
int sum=0,i=1;//初始化以下循环所需的变量
当(i
它应该告诉我我问的前n个数字,例如n=5 结果6、28、496、8128、33550336

实际结果总是前四个。

只要等待足够长的时间就可以了

计算第三个数字需要30毫秒,计算第四个数字需要10秒

要达到20.000这个数字,已经需要53秒了。因此,对于33.000.000,可能需要87.450秒=24.3小时。甚至更长,因为它看起来不是线性的:

6 after  0.0
28 after  0.0
496 after  0.033
8128 after  10.38
10000 ...  5.54
20000 ...  53.01    (estimate 24 h)
30000 ...  132.63
40000 ...  244.09
50000 ...  387.71
60000 ...  563.20   (estimate 86 h)
70000 ...  771.75
80000 ...  1010.81  (estimate 115 h)


如果您告诉我们程序永远不会退出,那会很有帮助。

停止使用1个字符的变量名。获取一个IDE,为您完成代码。长变量名不需要更多的输入工作。如果你输入5,为什么它要输出8个数字?对不起,这只是我组织它们的方式,我实际上使用了一个可以完成代码的方法,这就是为什么我在每一行附近都加上注释的原因。仍然是一样的,我认为问题可能是变量的最大值,但是第六个数字应该会有问题,它仍然会向我显示第五个
33550336
需要大量迭代才能找到它的除数,更不用说所有数字的除数了that@SimoneNardone:你宁愿找一个更快的算法。我最后一次估计,在我取消这个实验之前是115小时。我真的没有考虑退出,这是一段很艰难的时间。