We need to choose representative for each combination. Sorted is the easiest to work with. We want to put in the smallest factor as much as possible before we move on to a larger factor.
Corrected Code
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<>();
if (n < 3) {
return result;
}
List<Integer> factors = new ArrayList<>();
factorSearch(result, factors, n, 2);
return result;
}
private void factorSearch(List<List<Integer>> result,
List<Integer> factors,
int dividend,
int startFactor) {
// base case
if (dividend == 1 && factors.size() > 1) {
result.add(new ArrayList<>(factors));
return;
}
// search
for (int factor = startFactor; factor < dividend + 1; factor++) {
if (dividend % factor != 0) {
continue;
}
factors.add(factor);
factorSearch(result, factors, dividend / factor, factor);
factors.remove(factors.size() - 1);
}
}