Let's say n = 4. With recursion I want to return:
1 1 1 1
1 1 2
1 3
2 1 1
2 2
3 1
4
Basically I want to take number n and with by combining numbers 1,2,3 and 4 create all possible variations when the number of sum == n.
This was my first idea, but it gives me
Exception in thread "main" java.lang.StackOverflowError
public static void test_2(String path, int sum, int n){
if(sum == n){
System.out.println(path);
} else {
test_2(path+"1 ", sum + 1, n);
test_2(path+"2 ", sum + 2, n);
test_2(path+"3 ", sum + 1, n);
test_2(path+"4 ", sum + 2, n);
}
}
1 2 1? You don't want it, or did you just miss it?