15-212-X : Homework Assignment 1
Due Wed Sep 11, 10:00 am (electronically); papers at recitation.
Maximum Points: 50 (+10 extra credit)
First, the factorial function on natural numbers is defined mathematically as follows:
0! | = | 1 | |
n! | = | n * (n-1)! | for n > 0 |
The factorial function is the basis for the first definition of binomial numbers. BN(n,r) denotes the binomial number for (n,r) or "n choose r". It is defined as follows:
BN(n,r) | = | n! --------- r! (n-r)! |
for 0 <= r <= n |
A way to calculate binomial numbers which does not require factorials is to use Pascal's triangle. The triangle has a 1 at its top, every other number in the Pascal triangle is defined as the sum of the numbers to the right and to the left above. If there isn't any number, it counts as zero.
pascal
and must have type int * int -> int
.
Calculate the following values PT(5,3)
and PT(7,1)
.
pascal
is a correct implementation of the binomial as
defined using factorials. Carefully state and check the boundary
conditions! You don't need to type your proof in (writing it by hand and
handing it in at recitation is usually faster and easier).
Binomial numbers have other somehow surprising properties:
BN(n,0) + .... + BN(n,n)
for an arbitrary
n. This sum can be written in closed form. Find the closed form, and prove
it correct. To find the closed form, write an ML program sumAll
which is of type int -> int
. Experiment with the program and
guess the closed form. Then prove by induction that your claim is correct.
The next problem is very similar to the previous one.
BN(n,0) - BN(n,1) + .... + (-1)^n * BN(n,n)
again for an arbitrary n. This sum can be written in closed
form. Find the closed form, and prove it correct. To find the closed form,
write an ML program sumNeg
which is of type int ->
int
. Experiment with the program and guess the closed form. Then
prove by induction that your claim is correct.
binomial
which uses factorials. This implementation is likely to lead to
rather frequent overflow exceptions. Test it to find some values of n and r
where the computation requires intermediate results which exceed the size
allowed by the machine representation of integers. Compare this to the
function pascal
. Consider how you might define a function to
compute binomials based on multiplication which works for a larger set of
natural numbers. [Of course, eventually the result will be too large to be
representable with machine integers and no further improvement is possible
without another implementation of integers.]
We expect to obtain an approximation value which is close to the integral. It should be clear from the graph above, that the approximation value might be slightly above or below the real value. It is one of the easier theorems of Calculus that with finer partitions we can hope for a more refined approximation result. You can see this, in the next picture where we make one refinement step by splitting every partition on the x-axis in two equal parts. The sum of the red rectangles determines now the approximation value. Note that the light blue areas have been counted for in the previous step, but not any more.
integrate : real -> (real -> real) -> (real * real) -> (int
* real)
integrate epsilon f (a,b)
approximates the integral of f
between a and b up to epsilon. It returns a a pair, where the first component
yields the current refinement level (n, not 2n), and the second the
approximation value, fulfilling the condition from above. Integrate
Hint: Use the context browser to access information about the
mathematical functions from the
Math
structure.
integrate0 : (real -> real) -> (real -> real)
integrate0 f
is the function g(x) which represents
the value of the integral of f between 0 and x, approximated with
bound epsilon.
/afs/andrew/scs/cs/15-212-X/studentdir/<your andrew id>/ass<number>