1 / 57

Understanding Recursion

Recursion is a powerful programming technique that provides elegant solutions to certain problems. This article introduces recursion and explores its application to factorials.

houcke
Download Presentation

Understanding Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Understanding Recursion

  2. Introduction • Recursion is a powerful programming technique that provides elegant solutions to certain problems.

  3. Introduction • Recursion is a powerful programming technique that provides elegant solutions to certain problems. • Recursion is a programming technique in which a method calls itself either directly, or indirectly through another method.

  4. A Mathematical Example -Factorials • Mathematical formulas often are expressed recursively.

  5. A Mathematical Example -Factorials • Mathematical formulas often are expressed recursively. • In the following example, we will look in depth at factorials.

  6. Definition of Factorial Factorials - ! The symbol for factorial is “!” - the exclamation mark. The factorial of a positive integer is the product of all nonnegative integers less than or equal to that number. Zero factorial is a special case and 0! = 1 From this definition, 5! is 120. 5! = 5 . 4 . 3 . 2 . 1 = 120 This formula often is defined recursively, for all nonnegative integers as: n! = n(n-1)! for n > 0; 0! = 1; Any number factorial is that number times the factorial of one less than that number.

  7. A Closer Look Now, let’s look at the expression, n! = n * (n-1)! for n > 0; 0! = 1 You will notice that n! subtracts 1 from n, then recomputes the factorial of n-1. This is the recursion.

  8. A Closer Look Now, let’s look at the expression, n! = n * (n-1)! for n > 0; 0! = 1 Also notice that the simplest case is 0! This is called the base case.

  9. Base Cases • Base cases are important. A recursive method can solve only a base case.

  10. Base Cases • Base cases are important. A recursive method can solve only a base case. • If the method is called with a base case, it returns a result. If the methods is called with something other than the base case, the recursive method will decide what part it can accomplish, and then call itself to solve the rest of the problem.

  11. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 To understand how to program recursively, we will convert the mathematical definition of factorial into code.

  12. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 To understand how to program recursively, we will convert the mathematical definition of factorial into code. We’ll start by creating a class, FactorialExample. public class FactorialExample { }

  13. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 For simplicity, we will add a main method. public class FactorialExample { }

  14. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 For simplicity, we will add a main method. The main method will create a FactorialExample object. public class FactorialExample { public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  15. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 We’ll add our recursive method, factorial. public classFactorialExample { public longfactorial(long number) { } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  16. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 We now need to identify the base case; that is, the case the method factorial can solve without calling itself. public classFactorialExample { public longfactorial(long number) { } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  17. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case. public classFactorialExample { public longfactorial(long number) { } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  18. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case. public classFactorialExample { public longfactorial(long number) { if (number == 0) return1; } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  19. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case. However, 1! also equals 1. We can take advantage of this and change the code. public classFactorialExample { public longfactorial(long number) { if (number == 0) return1; } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  20. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case. However, 1! also = 1. We can take advantage of this and change the code. public classFactorialExample { public longfactorial(long number) { if(number <= 1) return1; } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  21. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 Now, we need to add recursion. We will look at the first part of the formula, n · (n-1)! If number is greater than 1, we need to compute n · (n-1)! public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  22. Converting to Code n! = n · (n-1)! for n > 0; 0! = 1 Now, we need to add recursion. We will look at the first part of the formula, n · (n-1)! If number is greater than 1, we need to compute n · (n-1)! public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  23. Examining the Code The best way to understand recursion is to step through the code. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } }

  24. Examining the Code The best way to understand recursion is to step through the code. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); } } We will use 5! as our test case.

  25. Examining the Code The best way to understand recursion is to step through the code. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + “! = “ + answer); } We will use 5! as our test case, and modify main slightly.

  26. Stepping through the Code The code starts by creating a FactorialExample object, fact. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  27. 5 - Stepping through the Code The testNumber variable is created and set to 5. The answer variable is created. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } testNumber answer

  28. testNumber 5 - answer Stepping through the Code The factorial method is called. public classFactorialExample { public longfactorial(longnumber) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  29. number testNumber 5 - 5 answer Stepping through the Code The formal parameternumber is created. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  30. number testNumber 5 - 5 answer Stepping through the Code The formal parameternumber is not less than or equal to 1. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else returnnumber * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  31. number number testNumber 5 - 5 5 answer Stepping through the Code This line is the recursive call. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  32. return: 5 * number number testNumber 5 - 5 5 answer Stepping through the Code This line is the recursive call. The method will return the value of number (in this case, 5), multiplied by . . . public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  33. return: 5 * number number testNumber 5 - 5 5 answer Stepping through the Code This line is the recursive call. The method will return the value of number (in this case, 5), multiplied by . . . The result of the method’s recursive call to itself. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  34. return: 5 * number number number testNumber 5 - 5 5 4 answer Stepping through the Code The factorial method is called, and another formal parameter number is created. This time the value of number is the previous formal parameter’s value (number - 1) or 4. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  35. return: 5 * number number number testNumber 5 - 5 5 4 answer Stepping through the Code The formal parameternumber is not less than or equal to 1. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); }

  36. return: 5 * number number number testNumber 5 - 5 5 4 answer Stepping through the Code So, the method will return the value of number (in this case, 4), multiplied by . . . public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 *

  37. return: 5 * number number number testNumber 5 - 5 5 4 answer Stepping through the Code So, the method will return the value of number (in this case, 4), multiplied by . . . The result of the method’s recursive call to itself. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 *

  38. return: 5 * number number number number testNumber - 5 3 5 4 5 answer Stepping through the Code Another formal parameter number is created. This time the value of number is the previous formal parameter’s value (number - 1) or 3. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 *

  39. return: 5 * return: 3 * number number number number testNumber 4 5 3 - 5 5 answer Stepping through the Code The method returns 3* the result of another recursive call. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 *

  40. return: 3 * return: 5 * number number number number number testNumber 5 5 - 4 2 3 5 answer Stepping through the Code The method returns 3 * the result of another recursive call, with a new formal parameter. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 *

  41. return: 5 * return: 3 * return: 2 * number number number number number testNumber 3 2 5 5 5 4 - answer Stepping through the Code The method returns 2 * the result of another recursive call, public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 *

  42. return: 5 * return: 3 * number number number number number number testNumber 1 2 4 5 - 3 5 5 answer Stepping through the Code with a new formal parameter. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 2 * return: 4 *

  43. return: 5 * return: 3 * number number number number number number testNumber 1 2 4 5 - 3 5 5 answer Stepping through the Code The method finally can solve its base case. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 2 * return: 4 *

  44. return: 5 * return: 3 * number number number number number number testNumber 1 2 4 5 - 3 5 5 answer Stepping through the Code number is equal to 1. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 2 * return: 4 *

  45. return: 5 * return: 3 * number number number number number number testNumber 5 - 4 1 5 3 5 2 answer Stepping through the Code The method returns 1. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 1 return: 2 * return: 4 *

  46. return: 3 * return: 5 * number number number number number testNumber 3 2 5 5 5 4 - answer Stepping through the Code Control is returned to the calling method. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 2 * 1 return: 4 *

  47. return: 3 * return: 5 * number number number number number testNumber 3 2 5 5 5 4 - answer Stepping through the Code The calling method now can return a value, in this case ( 2 * 1 ) or 2. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 2 * 1 return: 4 *

  48. return: 5 * return: 3 * number number number number testNumber 4 5 3 5 - 5 answer Stepping through the Code Control is returned to the calling method. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } 2 return: 4 *

  49. return: 5 * return: 3 * number number number number testNumber 4 5 3 5 - 5 answer Stepping through the Code The calling method now can return a value, in this case ( 3 * 2 ) or 6. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } 2 return: 4 *

  50. return: 5 * number number number testNumber 5 - 5 5 4 answer Stepping through the Code Control is returned to the calling method. public classFactorialExample { public longfactorial(long number) { if (number <= 1) return1; else return number * factorial(number - 1); } public static voidmain (String args[]) { FactorialExample fact = new FactorialExample(); long testNumber = 5; long answer; answer = fact.factorial(testNumber); System.out.println(testNumber + "! = " + answer); } return: 4 * 6

More Related