CP 07 Recursion1
CP 07 Recursion1
Swaroop Joshi
✤ Be patient!
The question
considered now
✤ The slightly nasty thing about the free-lunch library is that you cannot
directly use them to solve your problems.
✤ The slightly nasty thing about the free-lunch library is that you cannot
directly use them to solve your problems.
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) {
return fl_factorial(n); This will not work!
}
Recognising the smaller problem
✤ Finding → 5!
✤ Smaller subproblem → 4!
int factorial(int n) {
int answer = 1;
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
return answer;
}
It’s free-lunch time!
int factorial(int n) {
int answer = 1;
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
return answer;
}
Does it work?
Trace it!
Program State
n = 5
int answer = 1;
n = 5
answer =
int factorial_n_minus_1 = fl_factorial(n - 1);
n =
answer =
factorial_n_minus_1 =
answer = n * factorial_n_minus_1;
n =
answer =
factorial_n_minus_1 =
return answer;
Program State
n = 5
int answer = 1;
n = 5
answer = 1
int factorial_n_minus_1 = fl_factorial(n - 1);
n =
answer =
factorial_n_minus_1 =
answer = n * factorial_n_minus_1;
n =
answer =
factorial_n_minus_1 =
return answer;
Program State
n =
answer =
factorial_n_minus_1 =
answer = n * factorial_n_minus_1;
n =
answer =
factorial_n_minus_1 =
return answer;
Program State
n = 5
int answer = 1;
n = 5
answer = 1
int factorial_n_minus_1 = fl_factorial(n - 1);
n = 5
answer = 1
factorial_n_minus_1 = 24
answer = n * factorial_n_minus_1;
n =
answer =
factorial_n_minus_1 =
return answer;
Program State
n = 5
int answer = 1;
n = 5
answer = 1
int factorial_n_minus_1 = fl_factorial(n - 1);
n = 5
answer = 1
factorial_n_minus_1 = 24
answer = n * factorial_n_minus_1;
n =5
answer = 120
factorial_n_minus_1 = 24
return answer;
Program State
n = 5
int answer = 1;
n = 5
answer = 1
int factorial_n_minus_1 = fl_factorial(n - 1);
n = 5
answer = 1
factorial_n_minus_1 = 24
answer = n * factorial_n_minus_1;
n =5
answer = 120
factorial_n_minus_1 = 24
return answer;
Almost done with the free-lunch!
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
Accounting for n = 0
int factorial(int n) {
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
}
Almost done with the free-lunch!
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) {
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
} Now this works for all allowed value of n
Oh! Did I mention…
Oh! Did I mention… There is no free-lunch!
Oh! Did I mention… There is no free-lunch!
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/ What do we do now?
int factorial(int n) {
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
}
Do we have a function that computes a factorial
Recursion that we can call here?
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) {
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
}
Do we have a function that computes a factorial
Recursion that we can call here?
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) { How about this one?
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = fl_factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
}
Recursion
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) {
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
}
Recursion
/**
* @brief Computes n! for the given n.
* Requires n >= 0 This is a recursive implementation of
*/ factorial, where we call the same
int factorial(int n) { function on a smaller subproblem
int answer = 1;
if (n > 0) {
int factorial_n_minus_1 = factorial(n - 1);
answer = n * factorial_n_minus_1;
}
return answer;
}
Recursion
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) {
int answer = 1;
if (n > 0) {
answer = n * factorial(n - 1);
}
return answer;
}
Recursion
/**
* @brief Computes n! for the given n.
* Requires n >= 0
*/
int factorial(int n) {
int answer = 1;
if (n > 0) {
answer = n * factorial(n - 1); A more elegant version
}
return answer;
}
Crucial theorem of recursion
Crucial theorem of recursion
✤ then your code is still correct when you replace every call to the
FreeLunch version with a recursive call to your own version
Theorem applied
✤ Then so is the code that makes a recursive call to (your own) factorial
Theorem applied
✤ Then so is the code that makes a recursive call to (your own) factorial
Smaller subproblem Finding the smaller instance of the data to pass to the recursion
Smaller subproblem Finding the smaller instance of the data to pass to the recursion
Body //??
Design a recursive function that…
Body //??
Test //??
Design a recursive function that…
/**
* @brief Reports the number of digits in the given integer.
* Requires num >= 0
*/
int count_digits(int num) {
int answer = 1;
return answer;
}
Design a recursive function that…
/**
* @brief Reports the number of digits in the given integer.
* Requires num >= 0
*/
int count_digits(int num) { Does it handle all possible
int answer = 1; values of num?
return answer;
}
Design a recursive function that…
/**
* @brief Reports the number of digits in the given integer.
* Requires num >= 0
*/
int count_digits(int num) {
int answer = 1;
if (num > 9) {
answer = 1 + fl_count_digits(num / 10);
}
return answer;
}
Design a recursive function that…
/**
* @brief Reports the number of digits in the given integer.
* Requires num >= 0
*/
int count_digits(int num) {
int answer = 1;
if (num > 9) {
answer = 1 + fl_count_digits(num / 10);
}
return answer;
}
Now? Remove scaffolding!
Design a recursive function that…
/**
* @brief Reports the number of digits in the given integer.
* Requires num >= 0
*/
int count_digits(int num) {
int answer = 1;
if (num > 9) {
answer = 1 + fl_count_digits(num
fl_ / 10);
}
return answer;
}