This assignment may be done in Java or in Lisp.
Write a recursive function peanoplus(int x, int y),
using only ++ and --, to perform addition according
to the following definition:
peanoplus(x, y) = x, if y = 0; peanoplus(x + 1, y - 1), otherwise.
Note that the ++ and -- operators must appear before the operands in the recursive call, so that the change will be made before the call; otherwise, the change will be made after the call, causing an infinite loop.
We can think of peanoplus as similar to an algorithm for adding together buckets of rocks: if the second bucket is empty, stop; otherwise move one rock from the second bucket to the first bucket and continue.
Can you think of an invariant (property that is always true) of peanoplus? What is the Big O of peanoplus? This function is naturally tail-recursive.
Although the factorial function could be used in implementing n choose k, this would be inefficient for large values of n and small k. We have also seen that factorial quickly overflows the available accuracy of the basic types in Java. n choose k has the value 1 when k = 0. We can algebraically rewrite the definition into the following form for k > 0:
Write a function choose(int n, int k), using a tail-recursive auxiliary function, to compute n choose k without using the factorial function. Note that the above equation has k terms in both the numerator and the denominator; multiply by the numerator term before dividing by the denominator term to ensure that the numerator is evenly divisible by the denominator term.
1 n = 0 1 1 1 2 1 1 3 3 1 1 4 6 4 1 n = 4
Each new level of the triangle has 1's on the ends; the interior numbers are the sums of the two numbers above them. Write a program binomial(int n) to produce a list of binomial coefficients for the power n using the Pascal's triangle technique. For example, binomial(2) = (1 2 1). You may write additional auxiliary functions as needed. binomial should be a recursive program that manipulates lists; it should not use (choose n k). Use the function (choose n k) that you wrote earlier to calculate (choose 4 k) for k from 0 through 4; what is the relationship between these values and the binomial coefficients?