# How do I compare integer lists to know which is the largest?

3

I have an algorithm to calculate a sequence of numbers (Collatz Conjecture) .

The problem is that I need all up to 1,000,000 (one million), from there to know what number creates the longest list, I can not think of good ideas, but even if it was so I can not implement them in the code, so how can I know which one? Is the list longer?

If you can give me a serious hand of help, I just want some idea, so that I can solve it, thank you.

This is my code:

``````public class Collatz14 {

public static void main(String[] args) {

}

public static void pruebaSecuencia(){

for (int i = 5; i < 1000000; i++) {

getSequence(i);
}

}

public static int getSequence(int num){

Vector secuencia = new Vector(10);

while(num != 1){  //Algoritmo para obtener la secuencia de numeros

if(num % 2 == 0){

num = num/2;

}

else if(num % 2 != 0){

num = (num * 3)+ 1;
}
}

return secuencia.size(); //Retorno el tamaño de la secuencia
}

}
``````

asked by Romulo Gallegos 12.08.2017 в 22:24
source

5

## Code

``````public class Main {

public static void iniciarEjercicio(int limite) {

/* Secuencia Mayor */
int numeroMayor = 0;

for (int i = 5; i < limite; i++) {

numeroMayor = i;
}

}

System.out.println("El numero que genera mas secuencia es el " + numeroMayor + " con " + cantidadSecuenciaMayor + " secuencias.");

}

public static int obtenerSecuencia(int numero) {

while (numero != 1) {

if (numero / 2.0 == numero / 2) {
numero /= 2;
} else {
numero = (3 * numero + 1) / 2;
}
}

}

public static void main(String[] args) {

/* Iniciamos el Programa */
iniciarEjercicio(1000000);

}
}
``````

## Explanation

I have modified your program a bit to make it a little easier, it is not necessary to use arrangements or anything like that, I will explain it to you part by part.

### First

We have created the function start `obtenerSecuencia()` , which receives a number as a parameter and returns the number of numbers found in this sequence.

``````public static int obtenerSecuencia(int numero) {

while (numero != 1) {

if (numero / 2.0 == numero / 2) {
numero /= 2;
} else {
numero = (3 * numero + 1) / 2;
}
}

}
``````

See? It only returns an integer variable, whose value is the number of sequences you will find for each `numero` .

### Second

We have created the function `iniciarEjercicio()` in which the limit to which our program will run will be specified. And in turn contains the algorithm responsible for determining which has been the number that obtained more sequences .

``````    public static void iniciarEjercicio(int limite) {

/* Secuencia Mayor */
int numeroMayor = 0;

for (int i = 5; i < limite; i++) {

numeroMayor = i;
}

}

System.out.println("El numero que genera mas secuencia es el " + numeroMayor + " con " + cantidadSecuenciaMayor + " secuencias.");

}
``````

## Online example!

Note: The online example runs only up to the number 1000, because if you run up to 1 million, it generates slowness in the compiler.

### Result

The number that generates the most sequence is 871 with 113 sequences.

source
3

You can save the sizes of the vectors in a Map within the function `pruebaSecuencia()` ,

``````Map<Integer, Integer> resultados = new HashMap();

public static void pruebaSecuencia(){
for (int i = 5; i < 1000000; i++) {
}
}
``````

Then you evaluate which is the largest.

Another alternative with the same code that you have can add two variables at the beginning and in the `pruebaSecuencia()` method you do the validation:

``````static int numero = 0;
static int tamanioSecuencia = 0;

public static void pruebaSecuencia(){
for (int i = 5; i < 1000000; i++) {
int temp = getSequence(i);
if(temp > tamanioSecuencia){
tamanioSecuencia = temp;
numero = i;
}
}
}
``````

Then just print the variables