ok, Gene has already mentioned that he is avoiding repeats. So, now we only need to understand his code.
Here is a try making it little more easy to understand: #include <stdio.h> int buf[1024]; int buffer_pos; void part(int N, int num_parts, int min) { int i; if (num_parts == 0 && N == 0) { for (i = buffer_pos-1; i>=0; i--) printf("%d ", buf[i]); printf("\n"); } else if (num_parts > 0) { for (i=min; i<=N; i++) { buf[buffer_pos++] = i; part (N-i, num_parts-1, i); buffer_pos--; } } } Idea is to fill the buf one position by amount i, then fill the rest of the buf with remaining number N-i (recursive call). The first if condition ends the recursion when there is no more N left to be partitioned and when num_parts is 0 The && in first condition is important and cannot be replaced with || Try replacing it to see an interesting output :) -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.