Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Recursion What is Recursion So what is Java recursion? In computer programming its the process of having a method continually call itself until a defined point of termination. public int myRecursiveMethod () { int aVariable = myRecursiveMethod(); } Well the first thing you should take note of is the name of the method: myRecursiveMethod. This is just a random name that was chosen to use for this method…nothing special going on there… But, take a look at what we’re doing inside the method: we’re calling a method named myRecursiveMethod. Notice anything special there? Yes, that’s the same method name! ……….. This code is missing a terminating condition, this is why it will run forever. So how about we include a terminating condition? public int myRecursiveMethod (int aVariable) { System.out.println(aVariable); aVariable--; if (aVariable == 0) return 0; return myRecursiveMethod(aVariable); } Now with this method, we’ve introduced a terminating condition. Can you spot it? When our int variable holds the value 0, then this method will not call itself and instead it will simply exit out of the flow. This can be seen from the return 0 statement. So now we’re in a position where this method will continually call itself with a decrementing value of aVariable. So once aVariable hits zero, our recursive method is done! Can you guess what the output would be if we called this method like so: myRecursiveMethod (10) So why use Java Recursion? • There are certain problems that just make sense to solve via Java recursion. This is the case because sometimes, when solving problems recursively, you can really cut down on code with your solutions. For example lets take a look at something called the Fibonacci sequence. Here are the first few numbers of this sequence: • 0, 1, 1, 2, 3, 5, 8, 13, 21… How do you ‘solve’ this problem with recursion in Java? • There are really only two things any recursive code needs to ensure that it will work properly: • A defined ending point. • A constant progression towards that ending point. • So long as you abide by these two rules, you’ll be okay. If you fail to abide by them, you might get caught in an infinite loop and you’ll have to manually terminate your program (not the end of the world). • Well then, what’s the defined “ending point” for our Fibonacci sequence? Well it will come in the form of the problem we wish to solve. The question would be something like this: “What is the 40th number in the Fibonacci sequence?”. So there we have it, that “40th” number is the ending point for our sequence. The first thing we need to do is think of how the Fibonacci sequence can be represented in terms of an equation. So if the first number plus the second number equals the third… and the second plus the third equals the fourth, then we can describe it like so: Fn = Fn-1 + Fn-2 Make sense? So the n in this case represents the index of any particular number in the sequence. Now obviously, we can’t just plug in the value of 40 for n and know what the answer is, because we need to start back at the very beginning and work our way up to n to figure it out. Since we need to work our way from the beginning of the equation, then that means we’ll likely need to start there with our coding. So how would our code start then? public static void main (String[] args) { int n1 = 0; int n2 = 1; System.out.println("n1="+n1); fibonacciSequence(n1, n2); } public static void fibonacciSequence(int n1, int n2) { System.out.println("n2="+n2); } That seems pretty good as a start, but there’s no recursion going on here… remember we need to call the fibonacciSequence method inside of itself to start Java recursion. The only problem is, if we do this now, it will run forever. Remember the two rules, first we need a clear progression towards an end (Fn = Fn-1 + Fn-2) and two we need an end! So what’s our ending going to be? Well, earlier I arbitrarily chose to go to the 40th index of the equation, so let’s stick with that. If we are going to be keeping track of which ‘index’ we’re currently ‘at’ then let’s store it as an instance variable. we also need to keep track of our ending point, so let’s store that as an instance variable as well. private static int index = 0; private static int stoppingPoint = 40; public static void main (String[] args) { int n1 = 0; int n2 = 1; System.out.println("n1="+n1); fibonacciSequence(n1, n2); } public static void fibonacciSequence(int n1, int n2) { System.out.println("n2="+n2); Okay, so now we’ve established the starting point, the ending point, but not the recursion. So let’s put that in as well: private static int index = 0; private static int stoppingPoint = 40; public static void main (String[] args) { int n1 = 0; int n2 = 1; System.out.println("index: " + index + " -> " +n1); fibonacciSequence(n1, n2); } public static void fibonacciSequence(int n1, int n2) { System.out.println("index: " + index + " -> " + n2); if (index == stoppingPoint) return; index++; fibonacciSequence(n2, n1+n2); } private static int index = 0; private static int stoppingPoint = 40; public static void main (String[] args) { int n1 = 0; int n2 = 1; System.out.println("index: " + index + " -> " +n1); fibonacciSequence(n1, n2); } public static void fibonacciSequence(int n1, int n2) { System.out.println("index: " + index + " -> " + n2); if (index == stoppingPoint) return; index++; fibonacciSequence(n2, n1+n2); } Reference • https://howtoprogramwithjava.com/java-recursion/