174 Intermediate C Programming
In this case it possible to find a closed form formula: f (n) is expressed without f(n − 1)
appearing on the right side of the = sign.
f(n) = 2f(n − 1) + 1
= 4f(n − 2) + 2 + 1
= 8f(n − 3) + 4 + 2 + 1
= 16f(n − 4) + 8 + 4 + 2 + 1
= 2
k
f(n − k) + 2
k−1
+ 2
k−2
+ ... + 4 + 2 + 1
= 2
n−1
f(1) + 2
n−2
+ 2
n−3
+ ... + 4 + 2 + 1, when k = n − 1
= 2
n−1
+ 2
n−2
+ 2
n−3
+ ... + 4 + 2 + 1, because f(1) = 1
= 2
n
− 1
(12.7)
It is not always possible, or easy, to find closed form formulas for recursive equations.
Usually this requires a working knowledge of various series. Proving the answer is correct
requires mathematical induction.
12.4 Calculating Integer Partitions
A positive integer can be expressed as the sum of a sequence of positive integers. An
integer partition creates such a sequence of integers. For example, 5 can be broken into
the sum of 1 + 2 + 2 or 2 + 3. These two partitions use different numbers, and thus
are considered unique integer partitions. The order of the number in the partition is also
important. Thus, 1 + 2 + 2 and 2 + 1 + 2 are considered different integer partitions because
1 appears in different positions. Below are some example integer partitions:
1 = 1 2 = 1 + 1 3 = 1 + 1 + 1 4 = 1 + 1 + 1 + 1
= 2 = 1 + 2 = 1 + 1 + 2
= 2 + 1 = 1 + 2 + 1
= 3 = 1 + 3
= 2 + 1 + 1
= 2 + 2
= 3 + 1
= 4
This question wants to answer the number of different partitions for a positive integer
n. This problem can be solved by using the four-step approach solving recursive problems:
1. Identify the argument (or arguments) of a problem.
The number n is naturally the argument for the problem.
2. Express the solution based on the arguments.
Let f(n) be the number of different partitions for integer n.
3. Determine the simple case(s) when the solutions are “obvious”.
When n is 1, there is only one way to partition the number: itself. When n is 2, there
are two ways: 1 + 1 and 2. Thus, f (1) = 1 and f(2) = 2.
4. Derive the relationships between the complex case(s) and the simpler case(s).
When n is larger than 2, the solution selects the first number. It must be an integer
between 1 and n inclusively. After selecting the first number, we have to partition
the remaining portion of the number. Thus for each of the n possibilities for the first