Java 项目Euler#14:程序运行时间长

Java 项目Euler#14:程序运行时间长,java,Java,我正试图解决Euler 14项目: 以下是我尝试过的: public class Problem14 { public static void main(String[] args) { int longestLength = 1; int longestStart = 1; for (int i = 1; i < 1000000; i++) { int candidate = getCollatzLength

我正试图解决Euler 14项目:

以下是我尝试过的:

public class Problem14 {
    public static void main(String[] args) {
        int longestLength = 1;
        int longestStart = 1;
        for (int i = 1; i < 1000000; i++) {
            int candidate = getCollatzLength(i);
            if (candidate > longestLength) {
                longestLength = candidate;
                longestStart = i;
            }
        }
        System.out.println("starting point = " + longestStart);
    }

    public static int getCollatzLength(int startingNumber) {
        int length = 1;
        while (startingNumber != 1) {
            startingNumber = getNextCollatz(startingNumber);
            length++;
        }
        return length;
    }

    public static int getNextCollatz(int current) {
        if (current % 2 == 0) {
            return current / 2;
        } else {
            return 3 * current + 1;
        }
    }
}

有很多冗余的计算。例如,查找1000000的collatz序列的长度需要查找500000的collatz序列。您应该将所有中间结果存储在一个数组/列表中以减少冗余。

有很多冗余计算。例如,查找1000000的collatz序列的长度需要查找500000的collatz序列。您应该将所有中间结果存储在一个数组/列表中,以减少冗余。

当它达到113383时,链中的一些元素超过了2^31-1,因此超出了
int
的范围,并且在达到一百万之前还会发生许多其他情况。它将在一个链中命中的最大值是56991483520。如果您从
int
更改为
long
,您应该不会有问题。

当它达到113383时,链中的某些元素超过了2^31-1,因此超出了
int
的范围,并且在达到一百万之前还会发生许多其他情况。它将在一个链中命中的最大值是56991483520。如果您从
int
更改为
long
,您应该没事。

import java.util.*;
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int t=in.nextInt();
int结果=0;
int maxcount=0;
int[]arr=newint[(int)(5*Math.pow(10,6)+1)];
对于(int i=2;i最大计数){
结果=i;
最大计数=计数;
}
else if(count==maxcount){
结果=i;
}
arr[i]=结果;
}
对于(int a0=0;a03732423){
系统输出打印号(3732423);
}否则{
系统输出打印项次(arr[no]);
}
}
}
公共静态整数步数(长数值,整数计数){
while(num!=1){
如果(数值%2==0){
计数++;
num=num/2;
}
否则{
计数++;
num=num*3+1;
}
}
返回计数;
}
}

import java.util.*;
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int t=in.nextInt();
int结果=0;
int maxcount=0;
int[]arr=newint[(int)(5*Math.pow(10,6)+1)];
对于(int i=2;i最大计数){
结果=i;
最大计数=计数;
}
else if(count==maxcount){
结果=i;
}
arr[i]=结果;
}
对于(int a0=0;a03732423){
系统输出打印号(3732423);
}否则{
系统输出打印项次(arr[no]);
}
}
}
公共静态整数步数(长数值,整数计数){
while(num!=1){
如果(数值%2==0){
计数++;
num=num/2;
}
否则{
计数++;
num=num*3+1;
}
}
返回计数;
}
}
见此,见此
public class Problem14 {
    public static void main(String[] args) {
        int longestLength = 1;
        int longestStart = 1;
        for (int i = 1; i < 1000000; i++) {
            int candidate = getCollatzLength(i);
            if (candidate > longestLength) {
                longestLength = candidate;
                longestStart = i;
            }
        }
        System.out.println("starting point = " + longestStart);
    }

    public static int getCollatzLength(int startingNumber) {
        int length = 1;
        while (startingNumber != 1) {
            startingNumber = getNextCollatz(startingNumber);
            length++;
        }
        return length;
    }

    public static int getNextCollatz(int current) {
        if (current % 2 == 0) {
            return current / 2;
        } else {
            return 3 * current + 1;
        }
    }
}
new longest start is 106239
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();

        int result = 0;
        int maxcount = 0;

        int [] arr = new int[(int) (5 * Math.pow(10, 6) + 1)];
        for(int i=2;i<=3732423;i++) {
            int count = steps(i,0);
            if(count > maxcount) {
                result = i;
                maxcount = count;
            }
            else if(count == maxcount) {
                result = i;
            }
            arr[i] = result;
        }

        for(int a0 = 0; a0 < t; a0++){
            int no = in.nextInt();

            if(no > 3732423){
                System.out.println(3732423);
            }else{
                System.out.println(arr[no]);
            }
        }
    }


    public static int steps(long num,int count) {

        while(num !=1) {
            if(num % 2 == 0) {
                count++;
                num = num / 2;
            }
            else {
                count++;
                num = num*3 + 1;
            }
        }
        return count;

    }
}