1
I have the following statement of the problem:
A method that takes an integer as a parameter and returns a list of integers with their decomposed prime factors. As an example, if the input is number 36, the method returns a list containing [2, 2, 3, 3].
- I called the method on MAIN:
System.out.println(b(36));
- Expected result: [2,2,3,3].
I was able to implement the following method:
public static List<Integer> b(int x){
List<Integer> numeros = new ArrayList<Integer>();
int aux = x, i = 2, y = 0;
while (i <= x) {
if((primo(i) == true) && aux % i == 0){
aux = x / i;
numeros.add(y, i);
y++;
} else {
i++;
}
}
return numeros;
}
Error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3210) at java.util.Arrays.copyOf(Arrays.java:3181) at java.util.ArrayList.grow(ArrayList.java:261) at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235) at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227) at java.util.ArrayList.add(ArrayList.java:475) at desafio6.Desafio6.b(Desafio6.java:54) at desafio6.Desafio6.main(Desafio6.java:14) /Users/alissonfernando/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1 FALHA NA CONSTRUÇÃO (tempo total: 25 segundos)
And what is your doubt?
– Maniero
Like I said in the title, I’m not familiar with that kind of mistake so I got lost...
– A. Fernando
You don’t have enough memory to handle all this, probably because the logic is wrong and you’re generating a huge list.
– Maniero
Thanks friend, I will try to debug again... The question is open to new ideas...
– A. Fernando