27 July 2010, 2:46 pm
I really need help because I don't know why I can't see the recursion in this code while I can see understand recursion in most other problems. public class recursion { public static void main(String[] args) { int rings = 3; move(rings, 1, 3, 2); } public static void move(int rings, int source, int destination, int buffer) { if(rings > 0) { move(rings - 1, source, buffer, destination); System.out.println("Move ring "+rings+" from peg "+source+" to "+destination+"."); move(rings - 1, buffer, destination, source); } } } So here's what I'm doing to understand: move(3,1,3,2) Now since 3 > 0 move(2,1,2,3) Now since 2 > 0 move(1,1,3,2) Now since 1 > 0 move(0,1,2,3) 0 is not greater than 0 so it doesn't enter the if-block. I have no idea what to do from here. Am I even doing it correctly? Seeing the code in other languages isn't gonna help if I don't understand the recursion going on. I need help with that, please answer my original Q.... Read More »