Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这样不行?(项目问题5)_Java - Fatal编程技术网

Java 为什么这样不行?(项目问题5)

Java 为什么这样不行?(项目问题5),java,Java,我正在尝试我们的Euler项目,我遇到了问题5,问题是: 能被1到20的所有数整除的最小正数是多少 这就是我想到的: for (int x = 1; x >= 100100100; x++) { //100100100 is just a number to count to, an end point. int count = 0; for (int b = 1; b <= 20; b++) { if (x % b == 0) { //Tests n

我正在尝试我们的Euler项目,我遇到了问题5,问题是:

能被1到20的所有数整除的最小正数是多少

这就是我想到的:

for (int x = 1; x >= 100100100; x++) { //100100100 is just a number to count to, an end point.
    int count = 0;
    for (int b = 1; b <= 20; b++) {
        if (x % b == 0) { //Tests numbers.
            count++; //Counter

            if (count == 20) { //Numbers that fit between 1 and 100100100
                System.out.println(x);
            }
        }
    }
}
当我运行此命令时,它不会打印值。我做错了什么/忽略了什么?

将循环更改为:

for (int x = 1; x >= 100100100; x++) {
致:


实际上,您甚至没有进入循环体,因为在初始化值之后没有验证条件。

首先,问题真正需要的是找到最小的数字,该数字可以被1到20之间的所有数字平均整除。这意味着您不需要处理代码100100中的其他号码。你所需要做的就是找到范围内所有素数的最大幂,在这种情况下

2^4*3^2*5*7*11*13*17


.

您的最大值取小。您需要以不同的方式解决您的问题,因为蛮力将花费太长的时间,因为大多数project euler任务都会这样做,这是因为它们中的大多数都是数学挑战,而不是真正的编程挑战

你需要做些什么来计算出这个数字所包含的因素

2 => must be a multiple of 2
3 => must also be a multiple of 3
4 => must be a multiple of 2 and 2
5 => must be a multiple of 5
6 = > must be a multiple of 2 and 3

从2到6,你需要2*2*3*5,最小的因素将满足所有这些或60。在2到20之间,你需要考虑它们。注:它将是7、11、13、17和19的倍数。一旦你找到你需要的每个因素的多少,这不是你问题的答案,因为它已经被回答了,而是我的输入:

你应该这样做:

boolean flag = true;
if(x % b !=0){        // If one number does not meet the condition
    flag = false;     // A flag tells you the test failed
}
而不是计算每一项。还有一个while循环,可以数到无穷大


另外,解决方案扰流器警报:对于问题的解决方案,我的方法是使用素数和一些称为素数分解的东西。

条件不应该是x提示:您可以使用步骤20,而不是使用x++:当x==1和x>=100100同时出现时?我为什么要使用步骤20?只是好奇,谢谢你们的回答@大卫万岁谢谢你,只是在谷歌上搜索了答案;谷歌搜索?我刚刚在计算器中键入了16 x 9 x 5 x 7 x 11 x 13 x 17 x 19=。@DavidWallace我使用了aliatlii答案的剪切粘贴。这不起作用,因为正确答案大于100100100。我的答案是关于他的循环条件,而不是作为最大值使用的实际值。他可以使用Integer.max_值代替。但是是的,你是对的。
boolean flag = true;
if(x % b !=0){        // If one number does not meet the condition
    flag = false;     // A flag tells you the test failed
}