> >Another quick way of checking this is to add a trace statement: >each time you re-enter the recursive search function, print out the >depth of the recursion (you'll have to add a special variable to do >that), the current (partial) >solution, and all other parameters of the function call. This >should also quickly show you whether >or not your function is doing work it already did.
I'm resending the sample below with some sample trace statements. To write 8 as the sum of 4 squares, it would print something like this. The sums in the right col are not printed, but are there to guide you by showing what the computation 'means'. Printing the sum is possible, but may not be needed. I would suggest that any tracing you include show the current state (all previous sets) the current set - jeff parker tryslow(8, 4) tryslow(7, 3) 1 + tryslow(6, 2) 1 + 1 + tryslow(5, 1) 1 + 1 + 1 tryslow(4, 0) 1 + 1 + 1 + 1 tryslow(1,0) 1 + 1 + 1 + 4 tryslow(2,1) 1 + 1 + 4 tryslow(1,0) 1 + 1 + 4 + 1 tryslow(3, 2) 1 + 4 tryslow(2,1) 1 + 4 + 1 tryslow(1,0) 1 + 4 + 1 + 1 tryslow(4,3) 4 tryslow(3,2) 4 + 1 tryslow(2,1) 4 + 1 + 1 tryslow(1,0) 4 + 1 + 1 + 1 tryslow(0,2) 4 + 4 4 4 static boolean tryslow(int value, int num) { if (DEBUG) { for (int i = 4; i > 4; i--) System.out.print("\t"); System.out.println("tryslow(" + value + ", " + num + ")"); } if (value == 0) return true; // Are we done? // Base case: we have no more numbers to work with if (num == 0) return false; // Start at 1 and work up for (int i = 1; i*i <= value; i++) { int sq = i*i; // Place guess if (tryslow (value-sq, num-1)) { System.out.print(" " + sq); squares++; return true; } } return false; // Nothing worked. Backtrack }