我正在解决项目Euler的问题10,通过添加自然数来生成三角数的序列。所以第7个三角形数字是1 + 2 + 3 + 4 + 5 + 6 + 7 = 28,要找到第一个三角形数的值超过500个除数?
我写了以下程序:
public class ProjectEulerProb12HighestDivisorTriangNumb {
public static void main(String[] args) {
int minLim = 7; //As seven is already given in prob statement
System.out.println(
"The first triangular no to have more than 500 divisors is " + getNaturalnumb(minLim));
}
public static int getNaturalnumb(int natNum) {
int div = 1, TrianNum = 1;
while (div <= 500) {
natNum++;
TrianNum = getTrianNum(natNum);
div = getDivCount(TrianNum);
}
return TrianNum;
}
public static int getTrianNum(int n) { //Generating Triangular Number for any Natural no. n
int TrianNum = 0;
for (int i = 1; i <= n; i++) {
TrianNum += i;
}
System.out.println(" The current TrianNum is " + TrianNum);
return TrianNum;
}
public static int getDivCount(int n) {// Getting Divisor count for any Triangular no. n
int DivCount = 1, nextnum = 1;
while (n != nextnum || DivCount != 500) {
if (n % nextnum == 0) {
System.out.println(
" The current n is "
+ n
+ " Current nextnum is "
+ nextnum
+ " current Divcount is "
+ DivCount);
DivCount++;
}
nextnum++;
}
return DivCount;
}
}
我遇到了运行时异常:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ProjectEulerProb12HighestDivisorTriangNumb.getDivCount(ProjectEulerProb12HighestDivisorTriangNumb.java:47)
at ProjectEulerProb12HighestDivisorTriangNumb.getNaturalnumb(ProjectEulerProb12HighestDivisorTriangNumb.java:20)
at ProjectEulerProb12HighestDivisorTriangNumb.main(ProjectEulerProb12HighestDivisorTriangNumb.java:8)
要查看我错在哪里,我在方法getDivCount中输入print语句并得到以下输出:
The current TrianNum is 36
The current n is 36 Current nextnum is 1 current Divcount is 1
The current n is 36 Current nextnum is 2 current Divcount is 2
The current n is 36 Current nextnum is 3 current Divcount is 3
The current n is 36 Current nextnum is 4 current Divcount is 4
The current n is 36 Current nextnum is 6 current Divcount is 5
The current n is 36 Current nextnum is 9 current Divcount is 6
The current n is 36 Current nextnum is 12 current Divcount is 7
The current n is 36 Current nextnum is 18 current Divcount is 8
The current n is 36 Current nextnum is 36 current Divcount is 9
The current n is 36 Current nextnum is -36 current Divcount is 10
The current n is 36 Current nextnum is -18 current Divcount is 11
The current n is 36 Current nextnum is -12 current Divcount is 12
The current n is 36 Current nextnum is -9 current Divcount is 13
The current n is 36 Current nextnum is -6 current Divcount is 14
The current n is 36 Current nextnum is -4 current Divcount is 15
The current n is 36 Current nextnum is -3 current Divcount is 16
The current n is 36 Current nextnum is -2 current Divcount is 17
The current n is 36 Current nextnum is -1 current Divcount is 18
Exception in thread "main" java.lang.ArithmeticException: / by zero
为什么int值在满足条件(n!= nextnum)后递增为负值?帮助!
答案 0 :(得分:1)
原因是你正在击中整数溢出。
如果您通过循环(n = 36
),您会注意到DivCount = 9
时nextnum = 36
(即nextnum = n
)。这意味着在此迭代中循环不会破坏。
在所有其他nextnum
中,不等于n
,然后进入无限循环,直到nextnum
溢出并抛出异常。
答案 1 :(得分:0)
由于我无法发表评论,我必须发布答案。 我想补充一点:如果你需要非常大的数字,你需要使用BigInteger / BigDecimal等等 - 这也需要更多的代码来处理操作。但这是一个解决方案。