I have the following problem statement:
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 entry is number 36, the method returns a list containing [2, 2, 3, 3].
- I called the method in 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)