CS 6104 : Solutions to Homework Assignment 2
June 9, 1998

Problem 1. [Solution Courtesy of Yizhong Wang]

Chapter 4, Problem 14. You may assume the result in Problem 2.22. Use Mathematicato calculate the actual probability for

displaymath48

Put those results in a table that also gives the relative error if the probability is taken to be tex2html_wrap_inline149 .


We only need to show that

displaymath151

where tex2html_wrap_inline153 is defined to be 1 if tex2html_wrap_inline157 , and 0 otherwise.
But,

displaymath161

Clearly

displaymath163

and for tex2html_wrap_inline165 ,

displaymath167

hence,

displaymath169

From exercise 2.22, we know that

displaymath171

So,

displaymath173

This leads to

displaymath151

Use the following Mathematicacode,

y={0,0,0,0,0,0,0,0,0,0};
Do[Do[If[GCD[i,j]==1,y[[k]]++],{i,1,k*100},{j,1,k*100}],{k,1,10}];
Do[y[[i]]=y[[i]]/((i*100)^2),{i,1,10}];
k=10;e=6/(Pi^2);
TableForm[Table[{i*100,N[y[[i]],4],100*N[Abs[y[[i]]-e]/e]},{i,1,k}]]
get the table:

tabular92


Problem 2. [Solution Courtesy of Lynn Jones]

Implement the Extended Euclidean algorithm in Mathematica. (Mathematicahas the Euclidean and Extended Euclidean algorithms built in, but do not use those in your implementation.)

Let

eqnarray109

Show how your implementation can be used to find a solution tex2html_wrap_inline179 to the equation

eqnarray111

Give the Mathematicasteps to obtain one such solution.


Here is my Mathematicafunction definition for the Extended Euclidean algorithm as listed on page 72 of Bach & Shallit:

extEuclid[u_,v_] := {                             \
uVar = u;                                         \
varV = v;                                         \
matr = {{1,0},{0,1}};                             \
n = 0;                                            \
q = 0;                                            \
While[((uVar  != varV  ) && (varV  != 0)), {      \
    q = Floor[uVar /varV ];                       \
    matr = Dot[matr,{{q,1},{1,0}}];               \
    tempU = uVar ;                                \
    uVar =varV ;                                  \
    varV  = tempU-(q*varV);                       \
    n++;                                          \
}];                                               \
divisor = uVar ;                                  \
ufactor = (-1)^n*matr[[2,2]];                     \
vFact = (-1)^(n+1)*matr[[1,2]];                   \
answers = {divisor, ufactor, vFact} }
The function returns a list whose first (and only) member is a list whose members are the divisor d and multipliers a and b, such that d is the greatest common divisor of inputs u and v and au + bv = d. The only modifications to the algorithm that were made for the Mathematicaimplementation are the addition of the test for v==0 in the While loop condition, and the creation of variable copies for the inputs (Mathematicatreats the inputs as constants and won't assign to their identifiers).

We can use results of this function to solve the given equation. Let's examine the right side of the equation:

eqnarray113

Setting the coefficients of each c equal to the other, we solve for

eqnarray115

Here is one way to find a solution with Mathematica:




cs6104 class account
Tue Jun 9 14:43:35 EDT 1998