Sei sulla pagina 1di 4

PIC 10B HOMEWORK 6

Assignment

The function p(k, n) corresponds to an interesting mathematical structure, but in this


assignment we will simply compute its numerical value for various k and n using the following
recursive formula.
p(k, n) = p(k, n k) + p(k 1, n), 1 k n.
Now we need to establish what happens when n 0 or k 0.
p(k, n) = 0 for k < 0 OR n < 0. I.e., any negative input is automatically 0.
p(0, n) = 0 when n > 0.
p(k, 0) = 1 when k 0.
p(k, n) = p(n, n) when k > n.

Another recursive function of interest is known as the Stirling numbers of the first kind,
which are given by the recursion
s(k, n) = (n 1) s(k, n 1) + s(k 1, n 1), 1 k n,
with base cases:
s(k, n) = 0 for k < 0 or n < 0,
s(0, n) = 0 for n > 0,
s(k, 0) = 0 for k > 0,
s(k, n) = 0 for k > n,
s(0, 0) = s(n, n) = 1.

The Stirling numbers of the second kind are given by the recursion
S(k, n) = k S(k, n 1) + S(k 1, n 1), 1 k n,
with base cases:
S(k, n) = 0 for k < 0 or n < 0,
S(0, n) = 0 for n > 0,
S(k, 0) = 0 for k > 0,
S(k, n) = 0 for k > n,
S(0, 0) = S(n, n) = 1.

In addition, compute a file with factorial numbers, 1!, 2!, . . . , n!.

Create the functions for partitions, Stirling numbers of the first kind and Stirling numbers
of the second kind, and factorials using the Integer class from previous homework assign-
ments. The program should allow the user to input a positive integer n (you may assume
the input will indeed be a positive integer, no need to error check), and then creates files
named Partitions.txt, Stirling1.txt, and Stirling2.txt, that creates a table of all
1
2 PIC 10B DESALVO

values f (k, m) for 1 k n and 1 m n. Also create a file named Factorials.txt that
creates a column of all values 1!, 2!, . . . , n!, where each number is in a separate row. Note
that these values grow very large, which is why you must utilize an extended precision class.

What to Turn in
Place your code in a source file labeled hw6.cpp, with up to 19 additional files. As with
all programs in this course, your code should contain useful comments. In particular, your
name, the date, and a brief description of what the program does should appear at the top
of your source file.

Grading
Recursion Correctly implements a solution 5 points
Output File Correctly creates text files with the appropriate values 5 points
Solution Code is efficient but easy to follow 5 points
Style Variable names, comments, indentation 5 points
TOTAL 20 points

Note on grading: There is an automatic 5 point penalty for any homework that does not
compile.

A sample of output is below. NOTE! The values input are just an example. When grading
the homework the grader will select a value of m.

1 Welcome ! Please input a number m : 10


2
3 Creating Partition . txt file ... Done .
4 Creating Stirling1 . txt file ... Done .
5 Creating Stirling2 . txt file ... Done .
6 Creating Factorials . txt file ... Done .
7
8 Press any key to c o n t i n u e . . .

The entries in the first three files will consist of the following tabular data

f (1, 1) f (1, 2) f (1, 3) ... f (1, n)


f (2, 1) f (2, 2) f (2, 3) ... f (2, n)
f (3, 1) f (3, 2) f (3, 3) ... f (3, n)
... ... ...
f (n, 1) f (n, 2) f (n, 3) ... f (n, n)
PIC 10B DESALVO 3

The text file Partition.txt should look like the following:

1 1 1 1 1 1 1 1 1 1
1 2 2 3 3 4 4 5 5 6
1 2 3 4 5 7 8 10 12 14
1 2 3 5 6 9 11 15 18 23
1 2 3 5 7 10 13 18 23 30
1 2 3 5 7 11 14 20 26 35
1 2 3 5 7 11 15 21 28 38
1 2 3 5 7 11 15 22 29 40
1 2 3 5 7 11 15 22 30 41
1 2 3 5 7 11 15 22 30 42

The file Stirling1.txt should look like the following:

1 -1 2 -6 24 -120 720 -5040 40320 -362880


0 1 -3 11 -50 274 -1764 13068 -109584 1026576
0 0 1 -6 35 -225 1624 -13132 118124 -1172700
0 0 0 1 -10 85 -735 6769 -67284 723680
0 0 0 0 1 -15 175 -1960 22449 -269325
0 0 0 0 0 1 -21 322 -4536 63273
0 0 0 0 0 0 1 -28 546 -9450
0 0 0 0 0 0 0 1 -36 870
0 0 0 0 0 0 0 0 1 -45
0 0 0 0 0 0 0 0 0 1

The file Stirling2.txt should look like the following:

1 1 1 1 1 1 1 1 1 1
0 1 3 7 15 31 63 127 255 511
0 0 1 6 25 90 301 966 3025 9330
0 0 0 1 10 65 350 1701 7770 34105
0 0 0 0 1 15 140 1050 6951 42525
0 0 0 0 0 1 21 266 2646 22827
0 0 0 0 0 0 1 28 462 5880
0 0 0 0 0 0 0 1 36 750
0 0 0 0 0 0 0 0 1 45
0 0 0 0 0 0 0 0 0 1
4 PIC 10B DESALVO

The factorials will just be a single column of values

Factorials.txt

1
2
6
24
120
720
5040
40320
362880
3628800

NOTE: Do not worry about the digits not lining up for larger values of n. Just make sure
the formatting is correct and that there is exactly one space between each integer value, and
a return carriage between lines.
Due to the large amount of internal stack memory required for this recursion, you may
assume that the input value of n to your program will be less than 50. Larger values of n can
cause the program to crash or exit unexpectedly, due to the stack running out of memory
with all of the recursive calls required. (You should play around with large values of n just
to see what the limits are.)
In the comments for the functions, you may simply refer to the parameters as the larger
parameter and the smaller parameter.

Potrebbero piacerti anche