Contest 1
Recursive Functions
A definition that defines an object in terms of itself is said to be recursive. In computer science, recursion refers to a function or subroutine that calls itself, and it is a fundamental paradigm in programming. A recursive program is used for solving problems that can be broken down into sub-problems of the same type, doing so until the problem is easy enough to solve directly.
Examples
Fibonacci Numbers
A common recursive function that you’ve probably encountered is the Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, and so on. You get the next Fibonacci number by adding the previous two. Mathematically:
To be formal about this, we need to define when the recursion stops — called the base cases. The base cases for the Fibonacci function are and . The typical way to write this function is:
Here is a Python implementation of the Fibonacci function:
def Fibonacci(x):
if (x <= 1): return x
return Fibonacci(x-1) + Fibonacci(x-2)
Factorial Function
Consider the factorial function, , with defined as 1. We can define this recursively as:
Here is a Python implementation:
def Factorial(x):
if (x == 0): return 1
return x * Factorial(x-1)
Some Definitions
- Indirect recursion is when a function calls another function which eventually calls the original function.
- Single recursion is recursion with a single self-reference, such as the factorial example above.
- Multiple recursion, illustrated by the Fibonacci function, is when a function has multiple self-references.
- Infinite recursion is a recursive function that never returns because it keeps calling itself; the program will eventually crash with an out-of-memory error.
This ACSL category focuses on mathematical recursive functions rather than programming algorithms. While many mathematical functions can be computed iteratively more efficiently, many computer science algorithms must be written recursively.
Sample Problems
You will typically be asked to evaluate a recursive function for some specific value. Work your way down the recursive calls until there are no more calls, and then work your way back up.
Sample Problem 1
Problem: Find given the following:
Solution:
Working top-down:
Working back up:
Sample Problem 2
Problem: Find the value of given the following definition:
Solution:
Working back up:
Sample Problem 3
Problem: Find the value of given the following definition:
Solution:
Working back up:
Sample Problem 4
Problem: Consider the following recursive algorithm for painting a square:
- Given a square.
- If the length of a side is less than 2 feet, then stop.
- Divide the square into 4 equal size squares (draw a “plus” sign inside the square).
- Paint one of these 4 small squares.
- Repeat this procedure for each of the 3 unpainted squares.
If this algorithm is applied to a square with a side of 16 feet (total area 256 sq. feet), how many square feet will be painted?
Solution:
| Pass | Unpainted squares entering | Squares painted | Side length | Area painted |
|---|---|---|---|---|
| 1 | 1 | 1 | 8 ft | |
| 2 | 3 | 3 | 4 ft | |
| 3 | 9 | 9 | 2 ft | |
| 4 | 27 | 27 | 1 ft |
Total painted: sq. feet.
Online Resources
ACSL
Video Guide
Video Guide
Video Guide
Video Guide
Other Videos
Video Guide
Video Guide