Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 求第一个除数大于500的三角形数_Java_Factors - Fatal编程技术网

Java 求第一个除数大于500的三角形数

Java 求第一个除数大于500的三角形数,java,factors,Java,Factors,我试图用Java解决第12个Euler问题,但我似乎真的无法理解这里的问题。该脚本旨在输出第一个超过500个除数的三角形数,如代码注释中所述。正确的答案应该是“76576500”,而我的脚本输出的答案是“842161320”——相差很大。有人知道我哪里出错了吗?感谢您的帮助,谢谢 public class Script_012 { /* The sequence of triangle numbers is generated by adding the natural numbers.

我试图用Java解决第12个Euler问题,但我似乎真的无法理解这里的问题。该脚本旨在输出第一个超过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;
    }
}
}
公共类脚本\u 012
{
/*
三角形数的序列是通过自然数相加生成的,所以第七个三角形数是
1+2+3+4+5+6+7=28。前十个术语为:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
让我们列出前七个三角形数字的系数:
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
我们可以看到28是第一个有超过五个除数的三角形数。
第一个有500个以上除数的三角形数的值是多少?
*/
公共静态void main(字符串[]args)
{
布尔因子=false;
long num=1;
长流道=1;
因子的整数;
int最高_系数=0;
而(!足够的因素)
{
_因子的数量=0;
对于(int i=1;i<(int)Math.sqrt(num);i++)
{
如果((数量%i)==0)
{
_因子的数量+=1;
}
}
如果(系数的数量>500)
{
足够的系数=正确;
系统输出打印项数(num);
}
转轮+=1;
num+=跑步者;
}
}
}

问题在于,您只需添加小于或等于平方根的因子,但问题涉及所有因子,包括大于平方根的因子

简单(但缓慢)的解决方案:

将(int i=1;i<(int)Math.sqrt(num);i++)的
更改为(int i=1;i 500)的

{
足够的系数=正确;
系统输出打印项数(num);
}
转轮+=1;
num+=跑步者;
}
}

问题在于,您只需添加小于或等于平方根的因子,但问题涉及所有因子,包括大于平方根的因子

简单(但缓慢)的解决方案:

将(int i=1;i<(int)Math.sqrt(num);i++)的
更改为(int i=1;i 500)的

{
足够的系数=正确;
系统输出打印项数(num);
}
转轮+=1;
num+=跑步者;
}
}

太好了,谢谢你,伙计。我知道这与使用Math.sqrt而不是循环num有关,但在我的笔记本电脑上花的时间太长了。太好了,谢谢你。我知道这与使用Math.sqrt而不是通过num循环有关,但在我的笔记本电脑上花的时间太长了。
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 += 2;
            }
        }

        if(num % Math.sqrt(num) == 0) {
            num_of_factors++;
        }

        if (num_of_factors > 500)
        {
            enough_factors = true;
            System.out.println(num);
        }
        runner += 1;
        num += runner;
    }
}