以下是欧拉第7个问题的代码。
public static void main(String[] args) {
int desiredMax = 1000000;
int maxNumberOfTerms = 0;
int maxTerm = 0;
for(int startingNumber = 1; startingNumber<=desiredMax;startingNumber++) {
int numberOfTerms = 1;
int currentNumber = startingNumber;
while(currentNumber!= 1) {
if(currentNumber%2 == 0) {
currentNumber = currentNumber/2;
numberOfTerms++;
} else {
currentNumber = (3 * currentNumber) + 1;
numberOfTerms++;
}
}
if(numberOfTerms > maxNumberOfTerms) {
maxNumberOfTerms = numberOfTerms;
maxTerm = startingNumber;
}
}
System.out.print(maxNumberOfTerms + " " + maxTerm);
}
}
当我测试100,000代码时,代码正常工作,但当我启动它达到一百万时,它就会停止。有什么想法吗?
我添加了打印语句来打印每10,0000次迭代,并且在神秘停止之前它会达到大约110,000次。我不知道如果我有任何例外,或者我是,我不知道如何检查,对不起!
编辑:我再次尝试递归,但仍然遇到同样的问题。 package Euler;
public class Euler14 {
static int desiredMax = 1000000;
static int maxTerm = 0;
static int maxNumberOfTerms = 0;
static int currentNumber = 0;
static int numberOfTerms = 0;
public static void doMath(int startingNumber) {
if(startingNumber == 1) {
System.out.print( maxTerm + " " + maxNumberOfTerms);
}
else {
currentNumber = desiredMax;
while(currentNumber!= 1) {
if(currentNumber%2 == 0) {
currentNumber = currentNumber/2;
numberOfTerms++;
} else {
currentNumber = (3 * currentNumber) + 1;
numberOfTerms++;
}
}
numberOfTerms++;
if(numberOfTerms > maxNumberOfTerms) {
maxNumberOfTerms = numberOfTerms;
maxTerm = startingNumber;
}
desiredMax--;
doMath(desiredMax);
}
}
public static void main(String[] args) {
doMath(desiredMax);
}
}
答案 0 :(得分:2)
您的currentNumber
变量溢出int
数据类型,变为负数,导致代码进入无限循环。
要解决此问题,请声明变量(在原始循环代码中):
long currentNumber = startingNumber;
现在计算可以很快完成int desiredMax = 1000000
(一百万)。
当currentNumber
是数据类型int
时,这是导致无限循环的值的进展(从上到下,然后从左到右):
113383 1379177 2795998 204058582 -1812855948 -102088288 -4435 -248
340150 4137532 1397999 102029291 -906427974 -51044144 -13304 -124
170075 2068766 4193998 306087874 -453213987 -25522072 -6652 -62
510226 1034383 2096999 153043937 -1359641960 -12761036 -3326 -31
255113 3103150 6290998 459131812 -679820980 -6380518 -1663 -92
765340 1551575 3145499 229565906 -339910490 -3190259 -4988 -46
382670 4654726 9436498 114782953 -169955245 -9570776 -2494 -23
191335 2327363 4718249 344348860 -509865734 -4785388 -1247 -68
574006 6982090 14154748 172174430 -254932867 -2392694 -3740 -34
287003 3491045 7077374 86087215 -764798600 -1196347 -1870 -17
861010 10473136 3538687 258261646 -382399300 -3589040 -935 -50
430505 5236568 10616062 129130823 -191199650 -1794520 -2804 -25
1291516 2618284 5308031 387392470 -95599825 -897260 -1402 -74
645758 1309142 15924094 193696235 -286799474 -448630 -701 -37
322879 654571 7962047 581088706 -143399737 -224315 -2102 -110
968638 1963714 23886142 290544353 -430199210 -672944 -1051 -55
484319 981857 11943071 871633060 -215099605 -336472 -3152 -164
1452958 2945572 35829214 435816530 -645298814 -168236 -1576 -82
726479 1472786 17914607 217908265 -322649407 -84118 -788 -41
2179438 736393 53743822 653724796 -967948220 -42059 -394 -122
1089719 2209180 26871911 326862398 -483974110 -126176 -197 -61
3269158 1104590 80615734 163431199 -241987055 -63088 -590 -182
1634579 552295 40307867 490293598 -725961164 -31544 -295 -91
4903738 1656886 120923602 245146799 -362980582 -15772 -884 -272
2451869 828443 60461801 735440398 -181490291 -7886 -442 -136
7355608 2485330 181385404 367720199 -544470872 -3943 -221 -68
3677804 1242665 90692702 1103160598 -272235436 -11828 -662
1838902 3727996 45346351 551580299 -136117718 -5914 -331
919451 1863998 136039054 1654740898 -68058859 -2957 -992
2758354 931999 68019527 827370449 -204176576 -8870 -496
请注意,索引236处的最后一个值与索引218处的值相同。
答案 1 :(得分:0)
而不是迭代尝试使用递归函数方法来解决这个问题,使用强力算法最终将无法计算大量输入。