import java.util.ArrayList;
import java.util.List;
public class AllWaysToWriteANumberAsSumOfTwoOrMoreInts_Java {
public static void printList(List<Integer> list) {
if (list.size() != 1) {
for (int i = 0; i < list.size(); i++) {
if (i < list.size() - 1) {
System.out.print(list.get(i) + " + ");
} else {
System.out.print(list.get(i));
}
}
}
System.out.println();
}
public static void allWaysToWriteANumberAsSumOfTwoOrMoreInts(List<Integer> list, int i, int n) {
if (n == 0) {
printList(list);
}
for (int j = i; j <= n; j++) {
list.add(j);
allWaysToWriteANumberAsSumOfTwoOrMoreInts(list, j, n - j);
list.remove(list.size() - 1);
}
}
public static void main(String[] args) {
int n = 5; // The number
List<Integer> list = new ArrayList<>();
allWaysToWriteANumberAsSumOfTwoOrMoreInts(list, 1, n);
}
}
/*
run:
1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 2
1 + 1 + 3
1 + 2 + 2
1 + 4
2 + 3
*/