【发布时间】:2018-09-09 21:31:41
【问题描述】:
我正在尝试解决 Java 中的第 12 个欧拉问题,但我似乎真的无法理解这里的问题。该脚本旨在输出具有 500 多个除数的第一个三角形数,如代码中的注释中所述。正确答案应该是“76576500”,而我的脚本输出的答案是“842161320”——相差很大。有谁知道我哪里出错了?感谢所有帮助,谢谢!
public class Script_012
{
/*
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
*/
public static void main (String [] args)
{
boolean enough_factors = false;
long num = 1;
long runner = 1;
int num_of_factors;
int highest_factors = 0;
while (!enough_factors)
{
num_of_factors = 0;
for (int i = 1; i < (int) Math.sqrt(num); i ++)
{
if ((num % i) == 0)
{
num_of_factors += 1;
}
}
if (num_of_factors > 500)
{
enough_factors = true;
System.out.println(num);
}
runner += 1;
num += runner;
}
}
}
【问题讨论】: