Advertisement
amigojapan

recursion by vdamewood

Jul 10th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. CC-BY-4.0
  2. Recursion:
  3.  
  4. A recursive process is a process that can be expressed by using itself as
  5. a step. One case of recursion can be used to describe the process of
  6. calculating factorials. The factorial of a number is the product of
  7. multiplying all of the numbers from one to that number. For example, the
  8. factorial of 5 is 1 * 2 * 3 * 4 * 5, which is 120. Because a factorial is
  9. calculated as the product of all the numbers below it, it can also be
  10. calculated as a number, multiplies by the factorial of the number just below
  11. it. To extend the previous example, since the factorial of 4 is 1 * 2 * 3 * 4,
  12. the factorial of five is just five times that. Thus, if we set the factorial
  13. of one to one itself, then we can express all factorials as the product of
  14. n times the factorial of n-1. We would write this as a C function like this:
  15.  
  16. factorial(int n)
  17. {
  18. if (n == 1)
  19. return 1;
  20. else
  21. return factorial(n-1)*n;
  22. }
  23.  
  24. Another form of recursion is called tail recursion. With tail recursion, the
  25. the last step of using a process is to reuse the process itself. The above
  26. example, for calculating factorials is not tail recursive. The reason is that
  27. when you call factorial again, you still have one more step once you get its
  28. result. You still have to multiply its result by your current number.
  29.  
  30. For an example of a tail recursive process, consider the process of
  31. calculating the greatest common divisor of two numbers. If you have two
  32. numbers, a and b, through a trick of math, the greatest common divisor of
  33. the two numbers is also the greatest common divisor of b, and the remainder of
  34. divind a by b, unless they evenly divide, in which case you already have your
  35. answer. In C, we use % as a special symbol for the modulo operator, which
  36. yeilds the remainder in division. The process can be expressed with this C
  37. function:
  38.  
  39. int gcd(int a, int b)
  40. {
  41. if b == 0
  42. return a;
  43. else
  44. return gcd(b, a % b);
  45. }
  46.  
  47. If a and b evenly divide, then the remainder of dividing them will be zero,
  48. in which case the other value is the answer. If they don't divide evenly, we
  49. can call gcd again on the new values, to get the correct result. Consider this
  50. example, 9 and 12.
  51.  
  52. The first time we call this function, a will be 9, and b will be 12. Since b
  53. is not zero, call gcd(12, 9 % 12), where 9 % 12 is 9. This first step effectly
  54. swaps the values so that a is the greater value and b is the lower. Now we
  55. have gcd(12, 9). 12 % 9 is 3. So we try again with 9, and 3. With gcd(9, 3) we
  56. get another call of gcd(3, 0). Since b is 0, we return 3, and since each
  57. call is just returning the value return by the next call, they all end up
  58. returning 3.
  59.  
  60. Recursive functions are popular because they can be proven correct to solve
  61. certain problems through a process called proof by mathematical induction. In
  62. its simplest terms, you prove some base case, like the factorial of 1 is 1,
  63. and then you prove a case such that if one step is true, then that implies the
  64. next step is true. Such as showing that a factorial of n is the factorial of
  65. n-1 times n. By showing that these are correct, we can show that the factorial
  66. function works for all numbers from one to infinity. By doing it this way, bugs
  67. are easier to find, be cause we have reduced an infinite number of cases to
  68. just two. Tail recursion has the added benefit that since your just returning
  69. a value from a function call, the compiler can translate to simpler code that
  70. simply goes back to the beginning of the function, rather than by actually going
  71. through the slightly longer process of calling the function again.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement