|
| 1 | +# Decomposing a problem using recursion |
| 2 | + |
| 3 | +Following on from [finding the factorial of a number](/2014/04/01/factorial.html), here is the same problem solved in Python using recursion. |
| 4 | + |
| 5 | +#### Find the factorial of a number |
| 6 | + |
| 7 | +The mathematical definition of `n!` can be given as: |
| 8 | + |
| 9 | + n! = n x (n-1)! where n > 1 |
| 10 | + n! = 1 where n = 0 |
| 11 | + |
| 12 | +This is a *recursive* definition with a *base case*. |
| 13 | + |
| 14 | +The *recursive* definition defines factorial in terms of itself. This definition can be applied several times *recursively* to get the answer. |
| 15 | + |
| 16 | +The definition also includes a *base case*, which tells you when to stop. This base case is crucial. Without it the recursive definition would repeat endlessly. |
| 17 | + |
| 18 | +Here is an example: |
| 19 | + |
| 20 | + 4! = 4 x 3! apply the recursive definition |
| 21 | + 4! = 4 x 3 x 2! apply the recursive definition again |
| 22 | + 4! = 4 x 3 x 2 x 1! apply the recursive definition again |
| 23 | + 4! = 4 x 3 x 2 x 1 x 0! apply the recursive definition again |
| 24 | + 4! = 4 x 3 x 2 x 1 x 1 apply the base case |
| 25 | + 4! = 24 |
| 26 | + |
| 27 | +So, a recursive function is always defined in terms of itself and some base case where the final value is returned. |
| 28 | + |
| 29 | +We can do this in Python. |
| 30 | + |
| 31 | +#### Give the function a sensible name |
| 32 | + |
| 33 | + factorial |
| 34 | + |
| 35 | +#### Work out the inputs to the function and give them sensible names |
| 36 | + |
| 37 | + n |
| 38 | + |
| 39 | +#### Write out the first line of the function definition, including the arguments |
| 40 | + |
| 41 | + def factorial(n): |
| 42 | + |
| 43 | +#### Decide on the _base case_ |
| 44 | + |
| 45 | + n == 0 |
| 46 | + |
| 47 | +#### Add a test for the _base case_ |
| 48 | + |
| 49 | + print factorial(0) == 1 |
| 50 | + |
| 51 | +#### Add a return value for your function using the value that your test is expecting |
| 52 | + |
| 53 | + def factorial(n): |
| 54 | + if n == 0: |
| 55 | + return 1 |
| 56 | + |
| 57 | + print factorial(0) == 1 |
| 58 | + |
| 59 | +#### Run your test and check that it passes |
| 60 | + |
| 61 | + True |
| 62 | + |
| 63 | +#### Decide on the next case |
| 64 | + |
| 65 | + n == 1 |
| 66 | + |
| 67 | +In the case of a function like `factorial`, which computes a number that is part of a sequence, it is easy to decide on the each successive test case. In this case, the next value is _n! = 1_, where `n == 1`. |
| 68 | + |
| 69 | +#### Add another test using the next case |
| 70 | + |
| 71 | + print factorial(1) == 1 |
| 72 | + |
| 73 | +#### Decide on whether you are ready to compute a recursive solution |
| 74 | + |
| 75 | +Sometimes more than one base case is required before the general recursive solution can be found. But in this case the problem is a simple one: |
| 76 | + |
| 77 | +_For any value of n, multiply n by each predecessor until you reach the base case._ |
| 78 | + |
| 79 | +#### Sketch out the framework of a possible solution |
| 80 | + |
| 81 | +A general recursive solution often requires that: |
| 82 | + |
| 83 | + some function of n |
| 84 | + |
| 85 | +is combined with: |
| 86 | + |
| 87 | + some operator |
| 88 | + |
| 89 | +to a recursive call to the original function with |
| 90 | + |
| 91 | + some successor to n |
| 92 | + |
| 93 | +This schema will not work in every case, but is a sensible starting point. |
| 94 | + |
| 95 | +This is the heart of recursive programming. You are calling a function _within the function itself_. That is, you are calling it _recursively_. |
| 96 | + |
| 97 | +This is a very important and powerful technique in programming. |
| 98 | + |
| 99 | +#### Attempt to fill in the blanks |
| 100 | + |
| 101 | +This is where the big intuitive leap is required. |
| 102 | + |
| 103 | + n! = n * (n-1) * (n-2) * ... * 3 * 2 * 1 |
| 104 | + |
| 105 | +From the form of the solution to n!, probably the first thing to see clearly is that: |
| 106 | + |
| 107 | + some operator of n = * |
| 108 | + |
| 109 | +The second thing to see is that: |
| 110 | + |
| 111 | + some function of n = n |
| 112 | + |
| 113 | +This comes from the first term in the series. |
| 114 | + |
| 115 | +The final thing to see is that: |
| 116 | + |
| 117 | + some successor to n = n - 1 |
| 118 | + |
| 119 | +Which should be clear from the way in which each term of n! is one less than its predecessor. |
| 120 | + |
| 121 | +#### Now attempt a general solution to the problem |
| 122 | + |
| 123 | + def factorial(n): |
| 124 | + if n == 0: |
| 125 | + return 1 |
| 126 | + return n * factorial(n - 1) |
| 127 | + ^ ^ ^ |
| 128 | + function of n operator successor to n |
| 129 | + |
| 130 | + |
| 131 | +#### Add more test cases |
| 132 | + |
| 133 | + print factorial(0) == 1 |
| 134 | + print factorial(1) == 1 |
| 135 | + print factorial(2) == 2 |
| 136 | + print factorial(3) == 6 |
| 137 | + print factorial(4) == 24 |
| 138 | + |
| 139 | +#### Run the tests |
| 140 | + |
| 141 | + True |
| 142 | + True |
| 143 | + True |
| 144 | + True |
| 145 | + True |
| 146 | + |
| 147 | +Which confirm that this is probably a good general solution. |
| 148 | + |
0 commit comments