% Document Type: LaTeX
% Master File: ps3rsa.tex
\input ../6001mac

\def\fbox#1{%
  \vtop{\vbox{\hrule%
     \hbox{\vrule\kern3pt%
 \vtop{\vbox{\kern3pt#1}\kern3pt}%
 \kern3pt\vrule}}%
 \hrule}}
\usepackage{../epsfig}
\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\psetheader{Sample Problem Set}{RSA Encryption}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
{\bf Public-Key Encryption and Digital Signatures }
\end{center}

\medskip

The ideas of {\it public-key encryption} and {\it digital signatures}
were discovered only in 1976.  But they already play a fundamental
role as a way to achieve private communication in a world that relies
increasingly upon digital information.  Interestingly, the fact that
there are fast algorithms for exponentiation and for testing prime
numbers (sections 1.2.4--1.2.6 of the text) lies at the root of
RSA---the most popular method for implementing public-key encryption.
In this problem set you will implement a version of the RSA system.
By doing so, you will gain experience with some algorithms that
although simple, have immense practical importance.\footnote{This
problem set was designed in 1987 by Ruth Shyu and Eric Grimson and
revised in 1992 by David LaMacchia and Hal Abelson.} Section 1 of this
handout desccribes how the system works.  Section 2 contains exercises
that you should be prepared to discuss in tutorial.  Section 3
contains background for the lab assigment, and section 4 is the actual
lab assignment.

\section{1. The RSA System}

People have been using secret codes for thousands of years; for this
reason it is surprising that in 1976, Whitfield Diffie and Martin Hellman
at Stanford University discovered a major new conceptual approach to
encryption and decryption; {\it public-key cryptography}.\footnote{W.
Diffie and M. Hellman, ``New directions in cryptography,'' {\it IEEE
Transactions on Information Theory}, IT-22:6, 1976, pp 644--654.}

Cryptography systems are typically based on the notion of using {\it
keys} for encryption and decryption.  An {\it encryption key}
specifies the method for converting the original message into an
encoded form.  A corresponding {\it decryption key} describes how to
undo the encoding.  In traditional cryptographic systems, the
decryption key is identical to the encryption key, or can be readily
derived from it.  As a consequence, if you know how to {\it encrypt}
messages with a particular key then you can easily {\it decrypt}
messages that were encrypted with that key.

Diffie and Hellman's insight was to realize that there are
cryptographic systems for which knowing the encryption key gives no
help in decrypting messages; that is, for which there is no practical
way to derive the decryption key from the encryption key.  This is of
immense practical importance.  In traditional cryptographic systems,
someone can send you coded messages only if the two of you share a
secret key.  Since anyone who learns that key would be able to decrypt
the messages, keys must be carefully guarded and transmitted only
under tight security.  In Diffie and Hellman's system, you can tell
your {\it encryption} key to anyone who wants to send you messages,
and not worry about key security at all.  For even if everyone in the
world knew your encryption key, no one could decrypt messages sent to
you without knowing your {\it decryption key}, which you keep private
to yourself.  Diffie and Hellman called such a system a {\it
public-key} cryptography system.

A few months after Diffie and Hellman announced their idea, Ronald
Rivest, Adi Shamir, and Leonard Adelman at MIT discovered a workable
method for implementing it.  This {\it RSA cryptography system} has
remained the most popular technique for public-key cryptography.

\subsection{The theory behind RSA}

RSA uses integers to represent groups of characters\footnote{For
example, the ASCII standard representation of a character is a 7-bit
integer.  In this problem set we will represent a block of four
characters as a 28-bit integer ($0 \leq s < 2^{28}$) by concatenating
the ASCII codes of the four characters.} and uses special
functions that transform integers to integers.

In the RSA scheme, you select two large prime numbers, $p$ and $q$.
You then define
\begin{eqnarray}
n&=& pq\\
m&=&(p - 1)(q - 1).
\end{eqnarray}
You also select a number $e$, such that $\gcd(e,m)=1$.  Your {\it
public key}, which you can advertise to the world, is the pair of
numbers $n$ and $e$.  Anyone who wants to send you a message $s$
(represented by an integer) encrypts it using the following {\it RSA
transformation} defined by $n$ and $e$:

\smallskip
\centerline{encrypted message = $s$ to the power of $e$, modulo $n$}
or
\[S = (s^e) \bmod n.\]

If you receive an encrypted message $S$, you decrypt it by performing
another RSA transformation with $n$ and a special number $d$:

\smallskip \centerline{$s^\prime$ = encrypted message to the power of
$d$, modulo $n$} or \[s^\prime = (S^d) \bmod n.\]

The number $d$ is chosen to have the property that $s = s^\prime$ for
every message $s$,\footnote{Actually, this is true only if
$\gcd(s,n)=1$.  If $n$ is the product of two large primes, then almost
all messages $s<n$ will satisfy this.} namely,

\[s = (s^e)^d \bmod n.\]

It can be shown that the number $d$ that has this property is the one
for which
\begin{equation}
de=1 \bmod m
\end{equation}
that is, for which $d$ is the {\it multiplicative inverse} of $e$
modulo $m$.\footnote{This is a basic result in number theory , we'll
just ask you to take it on faith.} It turns out that it is easy to compute $d$
efficiently if you know $e$ and $m=(p-1)(q-1)$.

Thus, to generate a pair of RSA keys, you choose prime numbers $p$
and $q$, compute $n=pq$, choose $e$, and use this to compute $d$.
You publish the pair $n$ and $e$ as your public key, but keep $d$
secret to yourself.  People send you encrypted messages using the
pair $(n,e)$.  You decrypt these messages using the pair $(n,d)$.

The security of the RSA system is based on the fact that even if
someone knows $e$ and $n$, the most efficient way known for them to
decrypt a message is to factor $n$ to find $p$ and $q$, then use
these to compute $m$, then use $e$ and $m$ to compute $d$.

That is to say, cracking an RSA code is, as far as anyone knows, as
difficult a computational problem as factoring $n$ into its prime
factors $p$ times $q$.  And although there has been a tremendous
amount of research on factoring, factoring arbitrary large numbers is
not a computationally feasible task.  For example, factoring $n=pq$
where $p$ and $q$ are each 200-digit primes, even with the today's
best factoring algorithms, would require running for more than 100
years on today's fastest supercomputers.\footnote{No one has actually
proved that cracking an RSA code is as difficult a problem as
factoring, but no other method for cracking these codes has been
discovered.  In addition, some computer scientists believe that it
may be possible to prove that there can be no fast (e.g., logarithmic
time) algorithms for factoring.  Given the popularity of RSA, the
discovery of such an algorithm would result in a massive security
breakdown for banks, businesses, and other organizations that use
RSA.}

\subsection{Digital signatures; Encrypting and signing}

In their 1976 paper, Diffie and Hellman suggested applying public-key
encryption to solving another important problem of secure
communication.  The problem is this: suppose you want to send a
message by electronic mail.  How can people who receive the message be
sure that it really comes from you---that it is not a forgery?  What
is required is some scheme for marking a message in a way that cannot
be forged.  Such a mark is called a {\it digital signature}.

Diffie and Hellman's suggestion was to proceed as follows: take the
message and apply a publicly agreed upon {\it compression function}
(also called a {\it hash function}) that transforms the message to a
single, relatively small number.  In general, there will be many
messages that produce the same hash value.  Now transform the hash
value using your private key.  The transformed hash value is your
digital signature, which you transmit along with the message.  Anyone
who receives a message can authenticate the signature by transforming
it using your {\it public key} and checking that this gives the same
result as applying the compression function to the message.

The reason this scheme works is that anyone who wants to forge a
message claiming to be from you must produce a number that, when
transformed by your public key, matches the hash value.  Anyone can
compute the hash value, since the compression function is assumed to
be public.  But since you are assumed to be the only one who knows
your private key, only you can produce the number which is
transformed to the hash value by your public key.  Trying to forge a
digital signature is essentially the same task as trying to crack a
public-key encrypted message.

An even cuter idea works as follows: Suppose Barbara wants to send
George a signed message that only George will be able to read.  She
encrypts the message using George's public key.  Then she signs the
encrypted result using her own private key.  When George receives a
message that is supposed to be from Barbara, he first uses Barbara's
public key to authenticate the signature, then decrypts the message
using his own private key.  Figure~\ref{digital-signature} gives an
overview of the method.

\begin{figure}
\epsfig{file=./figure1.ps}
\caption {Encryption with digital signature.}
\label{digital-signature}
\end{figure}

Notice what this accomplishes: George can be sure that only someone
with Barbara's private key could have sent the message.  Barbara can
be sure that only someone with George's private key can read the
message.  This is accomplished without exchanging any secret
information between George and Barbara.  It's this capacity for
achieving secure communication without having to worry about
exchanging secret keys that makes public-key cryptography such an
important technique.

\subsection{Implementing RSA}

The primary thing we need in order to implement RSA is the fast
exponentiation algorithm from section 1.2.6 of the text:

\beginlisp
(define (expmod b e m)
  (cond ((zero? e) 1)
        ((even? e)
         (remainder (square (expmod b (/ e 2) m)) m))
        (else (remainder (* b (expmod b (- e 1) m)) m))))
\endlisp

\noindent
We'll assume that an RSA key is represented as a pair---modulus and exponent:

\beginlisp
(define make-key cons)
(define key-modulus car)
(define key-exponent cdr)
\endlisp

\noindent
The basic RSA transformation is then

\beginlisp
(define (RSA-transform number key)
  (expmod number (key-exponent key) (key-modulus key)))
\endlisp

\subsubsection{Generating prime numbers}

To generate RSA keys, we first of all need a way to generate primes.
The most straightforward way is to pick a random number in some
desired range and start testing successive numbers from there until we
find a prime.  The following procedure starts searching at a randomly
chosen integer between {\tt start} and ${\tt start}+{\tt range}$:

\beginlisp
(define (choose-prime smallest range)
  (let ((start (+ smallest (choose-random range))))
    (search-for-prime (if (even? start) (+ start 1) start))))
\null
(define (search-for-prime guess)
  (if (fast-prime? guess 2)
      guess
      (search-for-prime (+ guess 2))))
\null
(define choose-random
  ;; restriction of Scheme RANDOM primitive
  (let ((max-random-number (expt 10 18)))
    (lambda (n)
      (random (floor->exact (min n max-random-number))))))
\endlisp

The test for primality is the Fermat test, described in section 1.2.6:

\beginlisp
(define (fermat-test n)
    (let ((a (choose-random n)))
      (= (expmod a n n) a)))
\null
(define (fast-prime? n times)
    (cond ((zero? times) true)
          ((fermat-test n) (fast-prime? n (- times 1)))
          (else false)))
\endlisp

\subsubsection{Generating RSA key pairs}

Now we can generate a public RSA key and matching private key.  We'll
represent these as a pair:

\beginlisp
(define make-key-pair cons)
(define key-pair-public car)
(define key-pair-private cdr)
\endlisp

The following procedure generates an RSA key pair.  It picks primes
$p$ and $q$ that are in the range from $2^{14}$ to $2^{15}$ so that
$n=pq$ will be in the range $2^{28}$ to $2^{30}$, which is large
enough to encode four characters per number.\footnote{We're using
such small values of $n$ for this problem set because we want you to
play around with cracking an RSA system.  By starting with larger
random numbers, you can use the same method to produce a system that
really is secure.}  After picking the primes, it computes $n$ and $m$
according to equations (1) and (2).  It then chooses an
exponent $e$ and finds a number $d$ that satisfies equation (3).

\beginlisp
(define (generate-RSA-key-pair)
  (let ((size (expt 2 14)))
    (let ((p (choose-prime size size))
          (q (choose-prime size size)))
      (if (= p q)                 ;check that we haven't chosen the same prime twice
          (generate-RSA-key-pair) ;(VERY unlikely)
          (let ((n (* p q))
                (m (* (- p 1) (- q 1))))
            (let ((e (select-exponent m)))
              (let ((d (invert-modulo e m)))
                (make-key-pair (make-key n e) (make-key n d)))))))))
\endlisp

The exponent $e$ can be any random number $0<e<m$ with $\gcd(e,m)=1$.
The {\tt gcd} procedure is given in section 1.2.5 of the notes, but is
actually a Scheme primitive.

\beginlisp
(define (select-exponent m)
  (let ((try (choose-random m)))
    (if (= (gcd try m) 1)       ;if gcd is not 1, then try again
        try
        (select-exponent m))))
\endlisp

\subsubsection{Computing the multiplicative inverse}

The number $d$ required for the RSA key must satisfy
\[de=1 \bmod m\]
Using the definition of equality modulo $m$, this means that $d$ must
satisfy
\[km + de=1\]
where $k$ is a (negative) integer.  One can show that a solution to
this equation exists if and only if $\gcd(e,m)=1$.  The following
procedure generates the required value of $d$, assuming that we have
another procedure available which, given two integers $a$ and $b$,
returns a pair of integers $(x,y)$ such that $ax+by=1$.\footnote{The
Scheme primitive {\tt modulo}, which we use to insure a positive
result, is the same as {\tt remainder}, except on negative arguments:
{\tt (remainder -12 7)} is $-5$, while {\tt (modulo -12 7)} is 2.  In
general, {\tt (modulo a b)} always has the same sign as {\tt b}, while
{\tt (remainder a b)} always has the same sign as {\tt a}.}

\beginlisp
(define (invert-modulo e m)
  (if (= (gcd e m) 1)
      (let ((y (cdr (solve-ax+by=1 m e))))
        (modulo y m))   ;take y modulo m, in case y was negative
      (error "gcd not 1" e m)))
\endlisp

Solving $ax+by=1$ can be accomplished by a nice recursive trick that
is closely related to the recursive GCD algorithm in section
1.2.5 of the text.  Let $q$ be the quotient of $a$ by $b$, and let
$r$ be the remainder of $a$ by $b$, so that
\[a=bq+r\]
Now (recursively) solve the equation
\[b\bar{x}+r\bar{y}=1\]
and use $\bar{x}$ and $\bar{y}$ to generate $x$ and $y$.  We'll leave
to you the details of how to write the actual procedure.  (Ask in
recitation.)

\subsubsection{Encrypting and decrypting messages}

Finally, to use RSA, we need a way to transform between strings of
characters and numbers.  The code for this problem set includes
procedures {\tt string->intlist} and {\tt intlist->string} that
convert between character strings and lists of integers.  Each integer
(between 0 and $2^{28}$) encodes 4 successive characters from the
message.  If the number of characters is not a multiple of 4, the
message is padded by appending spaces:

\beginlisp
(string->intlist "This is a string.")
;Value: (242906196 69006496 245157985 217822450 67637294)
\null
(intlist->string '(242906196 69006496 245157985 217822450 67637294))
;Value: "This is a string.   "
\endlisp

\noindent
The code for these two procedures is included with the problem set
code, but you are not responsible for it.  You may want to look at it
if you are interested in how character strings can be manipulated in
Scheme.

To encrypt a message, we transform the message into a list of numbers
and convert the list of numbers using the RSA process together with
one key in the key pair.

\beginlisp
(define (RSA-encrypt string key1)
  (RSA-convert-list (string->intlist string) key1))
\endlisp

\noindent

You might guess that the right way to encode the list of numbers would
be to encode each number in the list separately.  But this doesn't
work well.  (See exercise 5 below.)  Instead, we encrypt the
first number, subrract that from the second number (modulo $n$) and
encrypt the result, add that to the next number and encrypt the
result, and so on, so that each number in the resulting encrypted list
will depend upon all the previous numbers:

\beginlisp
(define (RSA-convert-list intlist key)
  (let ((n (key-modulus key)))
    (define (convert l sum)
      (if (null? l)
          '()
          (let ((x (RSA-transform (modulo (- (car l) sum) n)
                                  key)))
            (cons x (convert (cdr l) x)))))
    (convert intlist 0)))
\endlisp

We'll leave it to you to implement the analogous {\tt
RSA-unconvert-list} procedure that undoes this transformation using
the other key in the key pair.  Then
we have

\beginlisp
(define (RSA-decrypt intlist key2)
  (intlist->string (RSA-unconvert-list intlist key2)))
\endlisp

Finally, to generate digital signatures for encrypted messages, we
need a standard compression function.  In this problem set, we'll
simply add the integers modulo $2^{28}$.\footnote{In practice, people
use more complicated compression schemes than this.  You might want to
think about why.}

\beginlisp
(define (compress intlist)
  (define (add-loop l)
    (if (null? l)
        0
        (+ (car l) (add-loop (cdr l)))))
  (modulo (add-loop intlist) (expt 2 28)))
\endlisp

\pagebreak

%==========================================================================
\section{2. Exercises}

\paragraph{Exercise 1:}
What is the difference between the following two ways of defining {\tt choose-prime}?\\
\beginlisp
(define (choose-prime smallest range)
  (search-for-prime (+ smallest (choose-random range))))
\null
(define choose-prime
  (lambda (smallest range)
    (search-for-prime (+ smallest (choose-random range)))))
\endlisp

\paragraph{Exercise 2:}
Using the representation for key pairs described above, draw the
box-and-pointer structure for a typical value returned by {\tt
generate-RSA-key-pair}.

\paragraph{Exercise 3:}
The method of sending secure signed
messages outlined above says that the sender should first encrypt the
message and then sign the result.  Would it be better to first sign
and then encrypt?

\paragraph{Exercise 4:}

The procedure {\tt RSA-encrypt} would be much simpler if we were
to encrypt each number in the list separately:

\beginlisp
(define (RSA-encrypt intlist key1)
    (map (lambda (int) (RSA-transform int key1))
         intlist))
\endlisp

\noindent
What would be the analogous {\tt RSA-decrypt} procedure?  Why is this
simple scheme inadequate for secure encryption?

\vfill
\newpage
%==========================================================================
\section{3. Background for a Programming Assignment}

\rule{6.5 in}{0.5 pt}

{\sf
\centerline{\Large\sf Ministry of Information}

To:   Ross (the Boss) \\
From: Rupert

So far we've been pretty successful.  I really liked the way you
arranged that cattle-futures deal, and the creative accounting by our
mole in the Rose Law firm has really done wonders.  But I'm getting
concerned about the security of our network.  My \$4M book deal with
the Salamander got out before the optimal moment.  I hope we haven't
been cracked by the entity in Fort Meade.
}

\rule{6.5 in}{0.5 pt}

{\sf
\centerline{\Large\sf Central Control}

To:   Rupert\\
From: Ross

You're absolutely right about the need for security.  I've gotten in
touch with some people I know at Family Values Communications.  FVC
markets a system that encrypts and authenticates messages using a
technique called RSA.  The FVC people say they can build an encryption
system for the modest fee of \$120M.
}

\rule{6.5 in}{0.5 pt}

{\sf
\centerline{\Large\sf Ministry of Information}

To:   Ross \\
From: Rupert

\$120 million?!?  {\it You have to be kidding.} That's almost as much
as it cost us to replace Gorby with Boris.  I contacted Chuck (the
Vest) at New England Research and Development (His cover is President
of MIT.) to ask his advice.  As you know, he helped us arrange the
White House mail system.\footnote{This is really true.  The electronic
mail connection to the White House was set up by people at the MIT AI
Lab.} Chuck says he can do the job for us, for a minor
consideration.  He needs help getting John (the German) installed in
the entity in Virginia.
}

\rule{6.5 in}{0.5 pt}

\centerline{MASSACHVSETTS INSTITVTE OF TECHNOLOGY}
\centerline{Office of the President}

Dear Albert and Gerry:

I have received a request of the {\it highest priority} asking that
6.001's next problem set involve RSA cryptography and digital
signatures.  Sorry for the rush.  I've managed to get some of the code
from Family Values Communications, so at least the students won't be
starting from scratch.  Thanks!
\vskip .2in
\centerline {Chuck Vest}

\rule{6.5 in}{0.5 pt}

%==========================================================================
\vfill
\newpage

\section{4. And now, for the programming assignment!}

Begin by loading the code for the problem set, using the Edwin command
{\tt M-x load-problem-set}.  This will load in Scheme some code that
was provided by ``friends'' at Family Values Communications, and also
place this code in an Edwin buffer for you to edit.  A listing is
attached to this problem set.

To test the code, evaluate

\beginlisp
(define test-public-key1 (key-pair-public test-key-pair1))
(define result1 (rsa-encrypt "This is a test message." test-public-key1))
\endlisp

\noindent
{\tt Result1} should be the list

\beginlisp
(209185193 793765302 124842465 169313344 117194397 237972864)
\endlisp

\noindent
{\tt test-key-pair1} is a sample RSA key pair that we have
generated for you to test your code with.  Keep in mind that punctuation
and upper vs. lower case are significant in the test string.

\paragraph{Exercise 1:}
Unfortunately, the code forwarded to us by President Vest is missing
one of the procedures---{\tt RSA-unconvert-list}---required to decrypt
messages.  Implement this procedure, which takes as arguments a list
of integers to decode and a decoding key, and returns a list of
integers, undoing the transformation implemented by {\tt
RSA-convert-list}.  Hint: This procedure is very similar in form to
{\tt RSA-convert-list}.  If you find yourself doing something much
more complicated, then you are barking up the wrong tree---ask
for help if necessary.

To test your procedure, try

\beginlisp
(define test-private-key1 (key-pair-private test-key-pair1))
\null
(RSA-unconvert-list result1 test-private-key1)
\endlisp

\noindent
You should obtain the result

\beginlisp
(242906196 69006496 213717089 229128819 205322725 67875559)
\endlisp

\noindent If that works, then you should be able to evaluate

\beginlisp
(RSA-decrypt result1 test-private-key1)
\endlisp

\noindent
to obtain the original test message (except for some
trailing spaces).  We've also supplied a second key pair for you to
work with, which you can obtain by evaluating

\beginlisp
(define test-public-key2 (key-pair-public test-key-pair2))
(define test-private-key2 (key-pair-private test-key-pair2))
\endlisp

\noindent
Turn in a listing of your procedure, the sample encryption
and decryption of the test message, and a sample encryption and
decryption (using {\tt test-key-pair1} and {\tt test-key-pair2}) of
some messages of your choice.

\paragraph{Exercise 2:}
Implement the method for encrypting and signing messages described in
section 1.  Start by specifying a (very) simple data structure called
a {\tt signed-message} that consists of a {\tt message} part and a
{\tt signature} part.  Now define a procedure {\tt encrypt-and-sign}
that takes as arguments a message to be encrypted and signed, the
sender's private key, and the recipient's public key.  The procedure
should encrypt the message, compute a digital signature for it, and
combine these to produce a signed message.

As a test, try

\beginlisp
(define result2
  (encrypt-and-sign "Test message from user 1 to user 2"
                    test-private-key1
                    test-public-key2))
\endlisp

\noindent
You should obtain a signed message whose message part is

\beginlisp
(499609777 242153055 12244841 376031918 242988502 31156692 221535122 463709109 468341391)
\endlisp

\noindent
and whose signature part is  15378444.

Now implement the inverse transformation {\tt
authenticate-and-decrypt}, which takes as arguments the received
signed message, the sender's public key, and the recipient's private
key.  If the signature is authentic the procedure should produce the
decrypted message.  If the signature is not authentic the procedure
should indicate this.  Test your procedures by trying

\beginlisp
(authenticate-and-decrypt result2 test-public-key1 test-private-key2)
\endlisp

\noindent
to recover the original message.  Turn in a listing of your
procedures together with a demonstration that they work. (Don't forget
to demonstrate that they catch non-authentic signatures.)

\paragraph{Exercise 3:}

The public key for sending messages to Bill Clinton is defined in the
problem set code:\\
\beginlisp
(define bill-clinton-public-key (make-key 833653283 583595407))
\endlisp

\noindent
The following public keys are also defined:\\
\beginlisp
(define al-gore-public-key (make-key 655587853 463279441))
(define bob-dole-public-key (make-key 507803083 445001911))
(define ross-perot-public-key (make-key 865784123 362279729))
(define hillary-clinton-public-key (make-key 725123713 150990017))
(define tipper-gore-public-key (make-key 376496027 270523157))
(define chuck-vest-public-key (make-key 780450379 512015071))
(define rupert-murdoch-public-key (make-key 412581307 251545759))
(define newt-gingrich-public-key (make-key 718616329 290820109))
\endlisp

\noindent
Yesterday Gingrich received the following message:\\
\beginlisp
(510560918 588076790 115222453 249656722 408910590 69814552
 690687967 281490047 41430131 256420885 184791295 75938032
 693840839 663727111 593617709 335351412)
\endlisp

\noindent
The signature was 65732336.  (These values are defined in the
problem set code as {\tt received\-mystery-message} and {\tt
received-mystery-signature}.)  Fortunately for us, a friend has
managed to obtain Gingrich's private key:

\beginlisp
(define newt-gingrich-private-key (make-key 718616329 129033029))
\endlisp

\noindent
Decrypt the message and identify who sent it.

\paragraph{Exercise 4:}
Our friends at FVC also sent us a procedure that generates RSA key
pairs: the public key and the associated private key.  But they are
missing the procedure that solves equations of the form $ax+by=1$.
Define this procedure, called {\tt solve-ax+by=1}.  It takes two
integer arguments $a$ and $b$ whose GCD is assumed to be 1.  It
returns a pair of integers $x$ and $y$.  Demonstrate that your
procedure works by finding integers $x$ and $y$ that satisfy the equation:
\[ 233987973x + 41111687y = 1\]
Don't forget to check your answer!

\noindent
If you have correctly defined this procedure, you should now be able
to call the procedure {\tt generate\-rsa-key-pair} (a procedure of
zero arguments) to produce randomly chosen key pairs.  Generate a key
pair for yourself.  Turn in a listing of your {\tt solve-ax+by=1}
procedure together with the values you found for the integers $x$ and $y$.
Also turn in a demonstration that your key pair can be used to encrypt and
decrypt messages.

\paragraph{Exercise 5:}
You now have a full implementation of an RSA cryptographic system,
complete with facilities for encryption, decryption, digital
signatures and signature authentication, and generating new keys.
Since the implementation uses such small primes, you should also be able to
{\it crack} the system.  In order to crack an RSA system, recall, you
must factor the modulus $n$ into its component prime factors $p$ and
$q$.  You can do this using the {\tt smallest-divisor} procedure that
is included in the code.\footnote{When you have found one prime
divisor $p$, the other divisor is $q=n/p$.} Write a procedure {\tt
crack-rsa} that, given a public key, returns the associated private
key.  Test your procedure using the pairs {\tt test-key-pair1} and
{\tt test-key-pair2} to show that it generates the correct private
keys, given the public keys.  Turn in a listing of your procedure,
together with demonstrations that it works.

\paragraph{Exercise 6:}
Bob Dole would like us to help him trick the Clinton administration
into taking unpopular stands.  Forge a message from Clinton to Gore,
asking Gore to announce that he and Clinton are plannning a major tax
increase.  Show the resulting message, the encryption, and the
signature, and demonstrate that the message will be decrypted by Gore
using his private key and Clinton's public key.

\paragraph{Exercise 7:}

Please prepare some appropriate forged messages between various people
whose public keys are listed above in lab exercise 3.  Demonstrate that
these messages will decrypt and autheticate correctly.  Be sure to say
who the message is (purportedly) from, and to whom it should be sent.

\paragraph{Exercise 8:}
The RSA system here is easy to crack because the primes are so small:
$n=pq$ is the product of two primes each about 5 digits long.  You
can use the supplied procedure {\tt timed} to see how long it takes
{\tt smallest-divisor} to find factors.  Evaluating, for example,

\beginlisp
(timed smallest-divisor 780450379)
\endlisp

\noindent
will find the smallest divisor of 780450379 and also print
how long the computation took in seconds.  Check how long it takes
to factor $n$ for some of the values produced by {\tt
generate-rsa-key-pair}.  Based on this data, estimate how long it would
take to crack an RSA code if we had used primes that were 50 digits
long; 100 digits long.  Give your answer in seconds, minutes, days,
or years, whichever seems most appropriate.

\end{document}


% ----------------------------------------------------------------
% Article Class (This is a LaTeX2e document)  ********************
% ----------------------------------------------------------------
\documentclass[11pt]{article}
\usepackage[english]{babel}
\usepackage{amsmath,amsthm, amssymb}
\usepackage{amsfonts}
\usepackage{multirow}
\usepackage{threeparttable}
\usepackage{enumerate}

%Candelaria's favorite packages
\usepackage{setspace}	% Set the spacing for the document
\usepackage{xtab}		% Use for tables
\usepackage{comment}	% Use for block comments
\usepackage{rotating}	% Helpful for rotating figures
\usepackage{lscape}	% Change a page into landscape
\usepackage{hyperref}
\hypersetup{colorlinks=false,pdfborder={0 0 0},breaklinks=true}
\usepackage{graphicx}	% Load a graphic image
%\usepackage{url}		% Use for formatting URLS (nice for NatBib also)
\usepackage{booktabs}  	% Use for nice tables/ Works well with ESTOUT
\usepackage{longtable} 	% Used to break long tables over multiple pages
\usepackage{caption}
\usepackage{subcaption} 	% Used to create SubFloats
\usepackage{listings} 	% Use to enter code blocks, like a fancy verbatim

% ADD COLOR:
\usepackage{color}
\usepackage[usenames, dvipsnames, svgnames, table]{xcolor}

% Packages for the bibliography
\usepackage[longnamesfirst]{natbib}
%\usepackage{natbib}

% Set Page Margins
%\usepackage{fullpage}
\addtolength{\oddsidemargin}{-.875in}
\addtolength{\evensidemargin}{-.875in}
\addtolength{\textwidth}{1.75in}
\addtolength{\topmargin}{-.875in}
\addtolength{\textheight}{1.75in}

% Set line and table spacing
\renewcommand{\baselinestretch}{1.0}
\renewcommand{\arraystretch}{1.1}
\setlength\abovedisplayskip{0pt}
\setlength\belowdisplayskip{0pt}

% Redefine the cite command
\renewcommand\cite{\citet}

%\setlength{\parindent}{0in}

%\setlength{\leftmargin}{0pt} \setlength{\parskip}{1.1ex} \setlength{\parindent}{1em} \setlength{\itemsep}{0pt} \setlength{\footnotesep}{10pt}
%\renewcommand{\footnoterule}{\rule{3in}{.3pt}\vspace{-.3pt}}

% THEOREMS -------------------------------------------------------
\newtheorem{thm}{Theorem}[section]
\newtheorem{cor}[thm]{Corollary}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\theoremstyle{definition}
\newtheorem{defn}[thm]{Definition}
\theoremstyle{remark}
\newtheorem{rem}[thm]{Remark}
%\numberwithin{equation}{section} %Number equations within section
% ----------------------------------------------------------------

\begin{document}

\begin{center}
{\huge Problem Set 2}\\[5pt]
{\Large Calculus}
\end{center}

\section*{Problem 1: Derivatives}
 Find the derivative of each expression with respect to $x$.
\begin{enumerate}[(a)]
\item $ \displaystyle y=55x$
\item $ \displaystyle y=\frac{1}{3}x^3$
\item $ \displaystyle y=(2x)^3$
\item $ \displaystyle y= 3x^7 - 20x^6 + 16x^5 - 4x^4 + 17x^3 - 20x^2 + 8x - 76$
\end{enumerate}

\section*{Problem 2: Examining Graphs of Derivatives}

For each expression, graph the original expression and the first two derivatives. Use these to identify:
\begin{enumerate}
	\item The location ($x$ value) of any roots of the original function or the first two derivatives.
	\item The location of any local minima or maxima in the original function or first derivative.
    \item Intervals on which the function or its first derivative is increasing or decreasing (use interval notation).
    \item The location of any inflection points in the original function
    \item Intervals on which the original function is concave up or concave down.

\end{enumerate}
\begin{enumerate}[(a)]
\item $ \displaystyle x^4-5x^3+4x^2+x+2$
\item $ \displaystyle \tan x$, considering only the interval $[0,2\pi]$.
\item $ \displaystyle e^x$
\item $ \displaystyle \sin x$, considering only the interval $[0,2\pi]$.
\end{enumerate}

Hint:  Desmos can be used to easily graph the original experession (in this case, $\sin x$ and the first two derivatives by graphing:
 	\[\sin x\]
    \[\frac{d}{dx}\sin x\]
    \[\frac{d}{dx}\frac{d}{dx}\sin x\]

\section*{Bonus: Fun Tasks}
\begin{enumerate}
\item Without looking it up, use Desmos to determine expressions for the first \em four \em derivatives of $\cos x$, in terms of $\sin x$ and $\cos x$.
\item A piece of wire 40 inches long is cut into two pieces.  One is bent into a circle and the other is bent into a square.  How should the pieces be cut to \em minimize \em the total area of the two shapes?
\end{enumerate}

\end{document}


\documentclass[t]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, tikz, pgfplots}
\pgfplotsset{compat = 1.16}
%\usetkzobj{all}

\title{Factoring Techniques}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
\maketitle
\end{frame}

\section{Find the greatest common factor of a quadratic expression.}

\begin{frame}{Greatest Common Factor}
The \alert{greatest common factor}, or GCF, of an expression is the largest term that divides into each term of that expression.	\newline\\	\pause

To find the greatest common factor of an expression:	\newline\\	\pause
\begin{itemize}
	\item Find the greatest common factor of the coefficients.	\newline\\	\pause
	\item Select the \underline{lowest power} of each unique variable.	\newline\\	\pause
\end{itemize}

Then divide each term by that GCF.
\end{frame}

\begin{frame}{Example 1}
Factor the GCF from each.	\newline\\
(a) \quad $16x^5 + 72x^3 - 36x^2$		\newline\\	\pause
GCF of 16, 72, and 36 is 4	\newline\\	\pause
Lowest power of $x$ is 2, so the GCF of the variables is $x^2$	\newline\\	\pause
GCF is $4x^2$	\newline\\	\pause
\[4x^2\left(4x^3+18x-9\right)\]
\end{frame}

\begin{frame}{Example 1}
(b) \quad $5a^4 + 70a^2 - 25a$ \newline\\ \pause
GCF of 5, 70, and 25 is 5 \newline\\	\pause
Lowest power of $a$ is 1, so the GCF of the variables is $a$ \newline\\ \pause
GCF is $5a$ \newline\\ \pause
\[5a\left(a^3+14a-5\right)\]
\end{frame}

\section{Factor trinomials in the form $x^2+bx+c$}

\begin{frame}{$x^2+bx+c$}
When factoring trinomials in the form $x^2+bx+c$, we are looking to find 2 numbers that {\color{blue}\textbf{multiply}} to make $c$ and {\color{red}\textbf{add}} to make $b$.	\newline\\	\pause

We can then write the factorization in the form
\[ (x+m)(x+n) \]
where $mn = c$ and $m+n=b$
\end{frame}

\begin{frame}{Example 2}
Factor each completely.	\newline\\
(a) \quad $x^2 + 15x + 36$	\newline\\
\onslide<2->{Product of the two numbers is 36.} \newline\\
\onslide<3->{Sum of the two numbers is 15.} \newline\\
\onslide<4->{Numbers are 3 and 12} \newline\\
\onslide<5->{\[(x+3)(x+12)\]}
\end{frame}

\begin{frame}{Visual Approach to Factoring}
$x^2 + 15x + 36$	\newline\\
\onslide<2->{
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) rectangle (4,4);
\draw (0,2) -- (4,2);
\draw (2,0) -- (2,4);
\onslide<3->{\node at (1,3) {$x^2$};}
\onslide<4->{\node at (0,3) [left] {$x$}; \node at (1,4) [above] {$x$};}
\onslide<5->{\node at (3,1) {$36$};}
\onslide<6->{\node at (3,4) [above] {3}; \node at (0,1) [left] {12};}
\onslide<7->{\node at (1,1) {$12x$}; \node at (3,3) {$3x$};}
\end{tikzpicture}
\end{center}	}
\onslide<8->{\[(x+3)(x+12)\]}
\end{frame}

\begin{frame}{Example 2}
(b) \quad $x^2 - 9x + 14$ \newline\\
\onslide<2->{Product of the two numbers is 14.} \newline\\
\onslide<3->{Sum of the two numbers is $-9$.} \newline\\
\onslide<4->{Numbers are $-7$ and $-2$} \newline\\
\onslide<5->{\[(x-7)(x-2)\]}
\end{frame}

\begin{frame}{Example 2}
(c) \quad $x^2+ 5x - 6$ \newline\\
\onslide<2->{Product of the two numbers is $-6$.} \newline\\
\onslide<3->{Sum of the two numbers is 5.} \newline\\
\onslide<4->{Numbers are $-1$ and 6} \newline\\
\onslide<5->{\[(x-1)(x+6)\]}
\end{frame}

\begin{frame}{Example 2}
(d) \quad $x^2 - 7x - 8$ \newline\\
\onslide<2->{Product of the two numbers is $-8$.} \newline\\
\onslide<3->{Sum of the two numbers is $-7$.} \newline\\
\onslide<4->{Numbers are $-8$ and 1} \newline\\
\onslide<5->{\[(x-8)(x+1)\]}
\end{frame}

\section{Factor trinomials in the form $ax^2+bx+c$}

\begin{frame}{$ax^2 + bx + c$}
To factor quadratic expressions in the form $ax^2+bx+c$, we will first \alert{transform} them into the form $x^2+bx+c$ by {\color{blue}\textbf{multiplying}} the values of $a$ and $c$ to get:	\newline\\	\pause

\[x^2 + bx + ac\]	\pause

We can then factor like in the last example, but we will have to remember to \textbf{transform the expression back}; as we will see.
\end{frame}

\begin{frame}{Example 3}
Factor each completely.	\newline\\
(a) \quad $3x^2 - 11x - 4$	\newline\\
\onslide<2->{Multiply $a=3$ and $c=-4$ to get $ac=-12$.}	\newline\\
\onslide<3->{Factor $x^2 - 11x - 12$}
\onslide<4->{\[(x-12)(x+1)\]}
\begin{center}
\onslide<5->{\Huge{\color{red}\textbf{We are not done yet!!}}}
\end{center}
\end{frame}

\begin{frame}{Example 3 \quad $3x^2 - 11x - 4$}
\[(x-12)(x+1)\]
\onslide<2->{Divide the $-12$ and $1$ by {\color{blue}\textbf{3}} and simplify.}
\onslide<3->{\[\left(x-\frac{12}{3}\right)\left(x+\frac{1}{3}\right)\]}
\onslide<4->{\[\left(x-4\right)\left(x+\frac{1}{3}\right)\]}
\onslide<5->{Slide any remaining denominators in front of their variable.}
\onslide<6->{\[(x-4)(3x+1)\]}
\end{frame}

\begin{frame}{Example 3}
(b) \quad $8x^2 + 2x - 3$ \newline\\
\onslide<2->{Multiply $a=8$ and $c=-3$ to get $ac=-24$}	\newline\\
\onslide<3->{Factor $x^2 + 2x - 24$}
\onslide<4->{\[(x+6)(x-4)\]}
\onslide<5->{\[\left(x + \frac{6}{8}\right)\left(x-\frac{4}{8}\right)\]}
\onslide<6->{\[\left(x + \frac{3}{4}\right)\left(x-\frac{1}{2}\right)\]}
\onslide<7->{\[(4x+3)(2x-1)\]}
\end{frame}

\begin{frame}{Example 3}
(c) \quad $16x^2 + 46x + 15$	\newline\\
\onslide<2->{Multiply $a=16$ and $c=15$ to get $ac=240$} \newline\\
\onslide<3->{Factor $x^2 + 46x + 240$}
\onslide<4->{\[(x+40)(x+6)\]}
\onslide<5->{\[\left(x+\frac{40}{16}\right)\left(x+\frac{6}{16}\right)\]}
\onslide<5->{\[\left(x+\frac{5}{2}\right)\left(x+\frac{3}{8}\right)\]}
\onslide<6->{\[(2x+5)(8x+3)\]}
\end{frame}

\begin{frame}{Example 3}
(d) \quad $7x^2 - 55x + 42$	\newline\\
\onslide<2->{Multiply $a=7$ and $c=42$ to get $ac=294$} \newline\\
\onslide<3->{Factor $x^2 - 55x + 294$}
\onslide<4->{\[(x-49)(x-6)\]}
\onslide<5->{\[\left(x-\frac{49}{7}\right)\left(x-\frac{6}{7}\right)\]}
\onslide<5->{\[\left(x-7\right)\left(x-\frac{6}{7}\right)\]}
\onslide<6->{\[(x-7)(7x-6)\]}
\end{frame}

\section{Use ``reverse engineering" to factor quadratic expressions}

\begin{frame}{Using the Graph}
You can also use the $x$-intercepts of the graph of the quadratic expression to help you factor.	\newline\\	\pause

For instance, the graph for $x^2 + 5x - 6$ has $x$-intercepts at $x=-6$ and $x=1$.

\begin{align*}
\onslide<3->{x &= -6 & x&= 1} \newline\\
\onslide<4->{x+6&=0 & x-1&= 0} \newline\\
\end{align*}
\begin{center}
\onslide<5->{$x^2+5x-6$ factors as $(x+6)(x-1)$}
\end{center}
\end{frame}

\begin{frame}{Using the Graph for $ax^2 + bx + c$}
For expressions in the form $ax^2 + bx + c$:	\newline\\	\pause
\begin{enumerate}
	\item<+-> Get the transformation expression by multiplying $a$ and $c$.	\newline\\
	\item<+-> Find those $x$-intercepts \newline\\
	\item<+-> Transform back by dividing by the value of $a$ and simplifying like in Example 3.
\end{enumerate}
\end{frame}

\begin{frame}{Using the Graph for $ax^2 + bx + c$}
For instance, we transformed $3x^2 - 11x - 4$ to $x^2 -11x - 12$	\newline\\	\pause
The $x$-intercepts of $x^2-11x-12$ are $x=-1$ and $x=12$	\newline\\	\pause
Divide each of those by 3 to get $x=-\frac{1}{3}$ and $x=4$ \pause	\newline\\
Move any remaining denominators in front of their variable: \pause \[3x=-1 \quad  \text{and} \quad x=4\] 	\pause
Get each equal to 0: \pause
\[3x+1 \quad \text{and} \quad x-4\]		\pause
\[(3x+1)(x-4)\]
\end{frame}

\end{document}


\documentclass[a4paper]{article}
\usepackage[inline]{enumitem}
\input{head}
\begin{document}

%-------------------------------
%	TITLE SECTION
%-------------------------------

\fancyhead[C]{}
\hrule \medskip % Upper rule
\begin{minipage}{0.295\textwidth}
\raggedright
\footnotesize
Winston Peng \hfill\\
50364686 \hfill\\
wpeng2@buffalo.edu
\end{minipage}
\begin{minipage}{0.4\textwidth}
\centering
\large
Homework 5\\
\normalsize
Intro To Discrete Structures, 2021\\
\end{minipage}
\begin{minipage}{0.295\textwidth}
\raggedleft
\today\hfill\\
\end{minipage}
\medskip\hrule
\bigskip

\section{Recurrence Relations (4 x 2 pts)}
\begin{enumerate}
    \item \textbf{A person deposits \$5000 in a bank account that yields 7\% interest compounded annually.}
    \begin{enumerate}
        \item \textbf{Set up a recurrence relation for the amount in the account at the end of \textit{n} years.}\\
        \boldmath $a_0 = $ \unboldmath $5000$\\
        \boldmath $a_n = $ \unboldmath $a_{n-1} * 1.07$
        \item \textbf{How much money will the account contain after 7 years? Show your calculation.}\\
        $a_0 = 5000$\\
        $a_1 = a_0 * 1.07 = 5000 * 1.07 = 5350$ \\
        $a_2 = a_1 * 1.07 = 5350 * 1.07 = 5724.5$ \\
        $a_3 = a_2 * 1.07 = 5724.5 * 1.07 = 6125.22$ \\
        $a_4 = a_3 * 1.07 = 6125.22 * 1.07 = 6553.99$ \\
        $a_5 = a_4 * 1.07 = 6553.99 * 1.07 = 7012.77$ \\
        $a_6 = a_5 * 1.07 = 7012.77 * 1.07 = 7503.66$ \\
        $a_7 = a_6 * 1.07 = 7503.66 * 1.07 = 8028.92$
    \end{enumerate}
    \item \textbf{Suppose that the number of bacteria in a colony doubles every hour.}
    \begin{enumerate}
        \item \textbf{Set up a recurrence relation for the number of bacteria after \textit{n} hours have elapsed.}\\
        \boldmath $a_n = $ \unboldmath $a_{n - 1} * 2$
        \item \textbf{If 150 bacteria are used to begin a new colony, how many bacteria will be in the colony in 6 hours? Show your calculation.}\\
        $a_0 = 150$\\
        $a_1 = a_0 * 2 = 150 * 2 = 300$\\
        $a_2 = a_1 * 2 = 300 * 2 = 600$\\
        $a_3 = a_2 * 2 = 600 * 2 = 1200$\\
        $a_4 = a_3 * 2 = 1200 * 2 = 2400$\\
        $a_5 = a_4 * 2 = 2400 * 2 = 4800$\\
        $a_6 = a_5 * 2 = 4800 * 2 = 9600$\\
    \end{enumerate}
\end{enumerate}

\bigskip %--------------------------

\section{Mathematical Induction (10 pts)}
\boldmath {\large \textbf{Prove for every positive integer \textit{n}, $1^3 + 2^3 + 3^3 + ... + n^3 = \frac{n^2(n+1)^2}{4}$ is true}} \unboldmath \\
P(n): $\sum_{i=1}^n i^3 = \frac{n^2(n+1)^2}{4}$\\

\textbf{\ul{Basis Step:}}\\
Goal: P(1) is true.\\
Case n = 1: $\sum_{i=1}^1 i^3 = 1$\\
$\frac{n^2(n+1)^2}{4} = \frac{1^2(1+1)^2}{4} = 1$\\
Therefore, P(1) is true.\\

\textbf{\ul{Inductive Step:}}\\
Goal: $\forall k \ge 1$, (P$(k) \implies $P$(k+1))$ is true. Let $k \ge 1$ \\

\ul{\textbf{Inductive Hypothesis:}}
Assume that P(\textit{k}) is true for an arbitrary integer \textit{k} with $k \ge 1$
\begin{center}
$P(k): \sum_{i=1}^k i^3 = 1^3 + 2^3 + ... + k^3 = \frac{k^2(k+1)^2}{4}$
\end{center}
    Under this assumption, we must show that p(k + 1) is true.
\begin{center}
    $P(k + 1): \sum_{i=1}^{k+1} i^3 = \frac{(k + 1)^2(k + 2)^2}{4}$
\end{center}
$\sum_{i=1}^{k+1} i^3 = \sum_{i=1}^{k} i^3 + (k + 1)^3 =
\frac{k^2(k + 1)^2}{4} + (k + 1)^3 = \frac{(k + 1)^2(k + 2)^2}{4}$\\
This shows that $P(k + 1)$ is true when $P(k)$ is true, therefore by principle of mathematical induction, for all $n \ge 1$, $P(n)$ is true.\\

% \frac{k^2(k + 1)^2 + 4(k + 1)^3}{4} =
% \frac{k^2(2k^2 + 2k + 1) + 4(k^3 + 3k^2 + 3k + 1^3)}{4} =
% \frac{2k^4 + k^22k + k^2 + 4k^3 + 12k^2 + 12k + 4}{4}$\\

\bigskip %------------------------------------------

\section{Strong Induction (10 pts)}
\ul{\textbf{Given:}} $a_0 = 0,\: a_1 = 4,\: a_n = 6a_{n - 1} - 5a_{n - 2}$ for $n \ge 2$\\
\ul{\textbf{Prove:}} $P(n): a_n = 5^n - 1$ for all $n \ge 0$\\

\ul{\textbf{Basis Step:}}\\
$P(0): a_0 = 0$,\quad $5^0 - 1 = 0$
$P(1): a_1 = 4$,\quad $5^1 - 1 = 4$
$P(0)$ and $P(1)$ are true.\\

\ul{\textbf{Inductive Step:}}\\
We must show that if P(i) is true for $0 \le i \le k$, where $k \ge 1$, then $P(k + 1)$ is true.\\

\ul{\textbf{Inductive Hypothesis:}} Let $k \ge 1$.\\
Assume $k \ge 1$ and P(i) is true for $0 \le i \le k$, where $k \le 1$ are true.
\begin{center}
    $P(k): a_k = 6a_{k - 1} - 5a_{k - 2}$
\end{center}
Assuming P(k), we must show P(k + 1).
\begin{center}
    $P(k + 1): a_{k + 1} = 5^{k + 1} - 1$
\end{center}
$a_{k + 1} = 6a_{k} - 5a_{k - 1}$, By recurrence relation\\
$a_{k + 1} = 6(5^k - 1) - 5(5^{k - 1} - 1)$, By inductive hypothesis\\
$a_{k + 1} = 5^{k + 1} - 1$\\
Therefore, $P(k + 1)$ is true when $P(i)$ is true for $0 \le i k$, where $k \le 1$, and the proof of the inductive step is complete.
% $a_{k + 1} = 6(6a_{k - 1} - 5a_{k - 2}) - 5(6a_{k - 2} - 5a_{k - 3}) =$

\section{Combinations and Permutations (10pts + 2pts Extra Credit)}
\begin{enumerate}
    \item \textbf{You want to make 4-letter codes using only the uppercase English letters. How many of these codes have no two consecutive letters the same?}\\
    $26 * 25 * 25 * 25 = 406250$
    \item \textbf{Consider the following sets of characters that you can use to make a password.}

    \textbf{Letters = \{b, q, s v, x\}} \\
    \textbf{Digits = \{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\}}\\
    \textbf{Specials = \{\#, \&, @, \$, *\}}

    \textbf{The passwords can be of length 6, 7, or 8. The first character cannot be a digit. How many passwords are there?}\\
    6 digit password: $10 * 20 * 20 * 20 * 20 * 20 = 32000000$\\
    7 digit password: $10 * 20 * 20 * 20 * 20 * 20 * 20 = 640000000$\\
    8 digit password: $10 * 20 * 20 * 20 * 20 * 20 * 20 * 20 = 12800000000$\\
    Total: 13472000000
    \item \textbf{Phone numbers are 7 digits long and start with either 716 or 718. How many different phone numbers are there in which the last four digits are all different?}\\
    $2 * 10 * 9 * 8 * 7 = 10080$
    \item \textbf{Bob has 5 hw assignments but can only do 3. How many ways can he choose 3?}\\
    $5 * 4 * 3 = 60$
    \item \textbf{Show that if 6 integers are chosen at random, at least two nums will have the same remainder when divided by 5. Use pigeonhole principle.}\\
    There are 6 integers, 6 - 1 = 5. Therefore, it stands to reason that two numbers that will have the same remainder when divided by five, as there are only 5 possible remainders of 5.
    \item \textbf{How many unordered selections of three elements can be made from the set \{a, b, c, d, e, f\}?}\\
    $6^3 = 216$
\end{enumerate}

\bigskip %-----------------------------

\section{Isomorphic (6pts = 3 x 2)}
\begin{enumerate}
    \item The graphs are isomorphic as the number of edges and vertices for the two graphs are identical.
    \item The graphs are not isomorphic as the vertices do not have the same degree between the two graphs.
\end{enumerate}

\bigskip %------------------------------

\section{Graphs (10pts = 2 x 5(+ 4 points extra)}
\begin{enumerate}
    \item Walk
    \item Walk, Circuit, Cycle
    \item Walk, Trail
    \item Walk
    \item Walk, Trail, Path
    \item Not walk
    \item Walk, Circuit
\end{enumerate}
\end{document}


\subsection{Reading Tabular Data into R}

Data used in statistical modeling are usually organized into tables,
often created using spreadsheet software. Most people
presume that the same software used to create a table of data should
be used to display and analyze it.  This is part of the reason for the
popularity of spreadsheet programs such as Excel and Google Spreadsheets.

\index{C}{Excel}
\index{C}{Google Spreadsheets}
\index{C}{spreadsheet software}
\index{P}{Importing data to R}
\index{P}{Data!importing to R}
\index{P}{ISMdata}
\index{P}{CSV files!reading}
\index{P}{Data!ISMdata@\texttt{ISMdata}*}
\index{P}{read.csv@\texttt{read.csv}}
\index{P}{Data!read.csv@\texttt{read.csv}}
\index{P}{reading CSV files}

For serious statistical work, it's helpful to take another approach that
strictly separates the processes of data collection and of data
analysis: use one program to create data files and another program to
analyze the data stored in those files.
By doing this, one guarantees that the original data are
not modified accidentally in the process of analyzing them.  This also
makes it possible to perform many different analyses of the data;
modelers often create and compare many different models of the same
data.

\subsubsection{Reading Tabular Data into R}

Data is central to statistics, and the tabular arrangement
of data is very common.  Accordingly, R provides a large number of
ways to read in tabular
data.  These vary depending on how the data are stored, where they are
located, etc.  To help keep things as simple as possible, the \pkg{mosaic}
package in R provides an operator, \function{fetchData} that allows
you to access data files in tabular format on your computer as well as data stored on
repositories such as the one associated with this book or one that a
course instructor might set up for his or her students.

\index{P}{fetchData}

\label{sec:loading-mosaic}
To use \function{fetchData}, as well as a variety of other operators
that you will encounter later, you need to load the \pkg{mosaic}
package.
\begin{Schunk}
\begin{Sinput}
> require(mosaic)
\end{Sinput}
\end{Schunk}
You need do this only once in each session of R, and on systems such
as RStudio the package will generally be reloaded automatically.
(If you get an error message, it's likely that the \pkg{mosaic} package
has not been installed on your system.  Use the package installation
menu in R to install \pkg{mosaic}, after which the \function{require}
function will load the package.)

Reading in a data table that's been connected with \function{fetchData}
is simply a matter of knowing the name of the data set.  For instance,
one data table used in examples in this book is
\texttt{swim100m.csv}.  To read in this data table and create an
object in R that contains the data, use a command like this:
\datasetSwimming
\begin{Schunk}
\begin{Sinput}
> swim = fetchData("swim100m.csv")
\end{Sinput}
\end{Schunk}

The \texttt{csv} part of the name in \texttt{"swim100m.csv"} indicates
that the file has been stored in a particular data format,
\newword{comma-separated values} that is handled by spreadsheet
software as well as many other kinds of software.

The part of this command that requires creativity is choosing a
name for the R object that will hold the data.  In the above command
it is called \texttt{swim}, but you might prefer another name, e.g.,
\texttt{s} or \texttt{sdata} or even \texttt{ralph}.  Of course, it's
sensible to choose names that are short, easy to type and remember,
and remind you what the contents of the object are about.

To help you identify data tables that can be accessed through
\function{fetchData}, examples in this book will be marked with a flag
containing the name of the file containing the data file.  The files
themselves are either built in to the \pkg{mosaic} package or
available automatically through the web site for this book.

\subsubsection{Data Frames}
\index{P}{data@\texttt{data}}
\index{C}{data frame|(}

The type of R object created by \function {fetchData} is called a
\newword{data frame} and is essentially a tabular layout. To illustrate , here are the first several cases
of the \texttt{swim} data frame created by the previous use of \function{fetchData()}:
\begin{Schunk}
\begin{Sinput}
> head(swim)
\end{Sinput}
\begin{Soutput}
  year time sex
1 1905 65.8   M
2 1908 65.6   M
3 1910 62.8   M
4 1912 61.6   M
5 1918 61.4   M
6 1920 60.4   M
\end{Soutput}
\end{Schunk}
Note that the \function{head} function, one of several functions that
operate on data frames, takes the R object that you
created, not the quoted name of the data file.

Data frames, like tabular data generally, involve variables and
cases.  In R, each of the variables is given a name.  You can refer to
the variable by name in a couple of different ways.  To see the
variable names in a data frame, something you might want to do to
remind yourself of how names a spelled and capitalized, use the
\function{names} function:
\begin{Schunk}
\begin{Sinput}
> names(swim)
\end{Sinput}
\begin{Soutput}
[1] "year" "time" "sex"
\end{Soutput}
\end{Schunk}
Another way to get quick information about the variables in a data
frame is with \function{summary}:
\begin{Schunk}
\begin{Sinput}
> summary(swim)
\end{Sinput}
\begin{Soutput}
      year           time      sex
 Min.   :1905   Min.   :47.8   F:31
 1st Qu.:1924   1st Qu.:53.6   M:31
 Median :1956   Median :56.9
 Mean   :1952   Mean   :59.9
 3rd Qu.:1976   3rd Qu.:65.2
 Max.   :2004   Max.   :95.0
\end{Soutput}
\end{Schunk}

To see how many cases there are in a data frame, use \function{nrow}:
\begin{Schunk}
\begin{Sinput}
> nrow(swim)
\end{Sinput}
\begin{Soutput}
[1] 62
\end{Soutput}
\end{Schunk}

\subsubsection{Variables in Data Frames}

\index{C}{data frame!variables of}
\index{C}{columns!in data frames}
\index{P}{columns!in data frame}
\index{C}{variables!in data frames}
\index{P}{variables!in data frame}
\index{P}{.@{\texttt{\$}}}
\index{P}{Data!access variables with \$}
Perhaps the most common operation on a data frame is to refer to the
values in a single variable.  The two ways you will most commonly use
involve functions with a \texttt{data=} argument and the direct use of
the \verb+$+ notation.

Most of the statistical modeling functions you will encounter in this
book are designed to work with data frames and allow you to refer
directly to variables within a data frame.  For instance:
\begin{Schunk}
\begin{Sinput}
> mean(year, data = swim)
\end{Sinput}
\begin{Soutput}
[1] 1952
\end{Soutput}
\begin{Sinput}
> min(year, data = swim)
\end{Sinput}
\begin{Soutput}
[1] 1905
\end{Soutput}
\end{Schunk}

The \texttt{data=} argument tells the function which data frame to pull the
variable from.  Leaving off that argument leads to an error:
\begin{Schunk}
\begin{Sinput}
> mean(year)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Error in mean(year) :
   error in evaluating the argument 'x'
\end{Soutput}
\end{Schunk}
% The following was replaced with the Schunk for formatting purposes.

Of course, you know that the variable \texttt{year}
is defined within the data frame \texttt{swim}, but you have to give
this information to the function \function{mean} explicitly, otherwise
it doesn't know where to find the variable.

You can also refer to the variable using the \verb+$+ notation, e.g.
\begin{Schunk}
\begin{Sinput}
> mean(swim$year)
\end{Sinput}
\begin{Soutput}
mean
1952
\end{Soutput}
\end{Schunk}
Think of this as referring to the variable by both its family name
(the data frame's name,``swim'') and its given name (``year''), something
like Einstein\$Albert.

The advantage of the \texttt{data=} approach becomes evident when you
construct statements that involve more than one variable within a data
frame.  For instance, here's a calculation of the mean year separately
for the different sexes:
\begin{Schunk}
\begin{Sinput}
> mean(year ~ sex, data = swim)
\end{Sinput}
\begin{Soutput}
  sex    S  N Missing
1   F 1951 31       0
2   M 1954 31       0
\end{Soutput}
\end{Schunk}
You will see much more of the \verb+~+ (``tilde'')
starting in Chapter \ref{chap:simple-models}.  It's the R notation for ``broken down by'' or ``versus.''

Both the \function{mean} and \function{min} functions know to look in
the data frame when interpreting variables, but not all R functions
--- particularly older ones --- are designed this way.\footnote{The
  ability of \function{mean}, \function{median}, and similar
  functions to handle the \texttt{data=} format is provided by the
  \pkg{mosaic} package.}  When you encounter a function that can't
handle the \texttt{data=} format, use the \texttt{\$} notation.
For example, this works
\begin{Schunk}
\begin{Sinput}
> mean(sqrt(year), data = swim)
\end{Sinput}
\begin{Soutput}
[1] 44.2
\end{Soutput}
\end{Schunk}
But not this:
\begin{Schunk}
\begin{Sinput}
> sqrt(year, data = swim)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Error in try(sqrt(year, data = swim)) : object 'year' not found
\end{Soutput}
\end{Schunk}

\index{P}{length@\texttt{min}}
\index{P}{mean@\texttt{mean}}
\index{P}{max@\texttt{max}}

\subsubsection{Adding a New Variable}

\index{C}{data frame!adding a new variable}
\index{C}{variable!adding to a data frame}

Sometimes you will compute a new quantity from the existing variables and
want to treat this as a new variable.  Adding a new variable to a data
frame can be done with the \texttt{\$} notation.
For instance, here is how to create a new variable in \texttt{swim}
that holds the \texttt{time} converted from minutes to units of seconds:
\begin{Schunk}
\begin{Sinput}
> swim$minutes = swim$time/60
\end{Sinput}
\end{Schunk}
The new variable appears just like the old
ones:
\begin{Schunk}
\begin{Sinput}
> names(swim)
\end{Sinput}
\begin{Soutput}
[1] "year"    "time"    "sex"     "minutes"
\end{Soutput}
\end{Schunk}
You could also, if you want, redefine an existing variable, for
instance:
\begin{Schunk}
\begin{Sinput}
> swim$time = swim$time/60
\end{Sinput}
\end{Schunk}
Such assignment operations do not change the original file from which
the data were read, only the data frame in the current session of R.
This is an advantage, since it means that your data in the data file
stay in their original state and therefore won't be corrupted by
operations made during analysis.

\subsection{Sampling from a Sample Frame}

\index{C}{sampling!with resample}
\index{C}{sampling!with shuffle}

Much of statistical analysis is concerned with the consequences of
drawing a sample from the population.
Ideally, you will
have a sampling frame that lists every member of the population from
which the sample is to be drawn.  With this in hand, you could treat
the individual cases in the sampling frame as if they were cards in a
deck of hands.  To pick your random sample, shuffle the deck and deal
out the desired number of cards.

When doing real work in the field, you would use the randomly dealt
cards to locate the real-world cases they correspond to.  Sometimes in
this book, however, in order to let you explore the consequences of
sampling, you will select a sample from an existing data set.  The
\function{deal} function performs this, taking as an argument the data
frame to be used in the selection and the number of cases to sample.

For example, the \texttt{kidsfeet.csv} data set \datasetFeet has
$n=39$ cases.
\begin{Schunk}
\begin{Sinput}
> kids = fetchData("kidsfeet.csv")
> nrow(kids)
\end{Sinput}
\begin{Soutput}
[1] 39
\end{Soutput}
\end{Schunk}
Here's how to take a random sample of five of the cases:
\begin{Schunk}
\begin{Sinput}
> deal(kids, 5)
\end{Sinput}
\begin{Soutput}
       name birthmonth birthyear length width sex biggerfoot domhand
8   Caitlin          6        88   23.0   8.8   G          L       R
17 Caroline         12        87   24.0   8.7   G          R       L
29     Mike         11        88   24.2   8.9   B          L       R
6    Scotty          3        88   25.7   9.7   B          R       R
20  Heather          3        88   25.5   9.5   G          R       R
\end{Soutput}
\end{Schunk}

\index{C}{sampling}
\index{C}{resampling!with replacement}
\index{P}{resample@\texttt{resample}*}
\index{P}{deal@\texttt{deal}*}
\index{P}{Resampling!resample@\texttt{resample}*}

The results returned by \function{deal} will never contain the same
case more than once, just as if you were dealing cards from a shuffled
deck.  In contrast, \code{resample} replaces each case after it is
dealt so that it can appear more than once in the result.  You
wouldn't want to do this to select from a sampling frame, but it turns
out that there are valuable statistical uses for this
sort of \newworddef{sampling with replacement}{sampling!with
  replacement}.  You'll make use of re-sampling in Chapter \ref{chap:statistical-inference}.

\begin{Schunk}
\begin{Sinput}
> resample(kids, 5)
\end{Sinput}
\begin{Soutput}
         name birthmonth birthyear length width sex biggerfoot domhand
15      Julie         11        87   26.0   9.3   G          L       R
31   Danielle          6        88   24.0   9.3   G          L       R
29       Mike         11        88   24.2   8.9   B          L       R
38     Hayley          1        88   21.6   7.9   G          R       R
29.1     Mike         11        88   24.2   8.9   B          L       R
\end{Soutput}
\end{Schunk}

Notice that Mike was sampled twice.


\section{Discussion}
\label{sec:discussion}
It has been observed throughout the experiments that all three models show rather similar performance on the housing price datasets under consideration. In order to investigate the contribution of categorical variables into making the final decision we explore the feature importances provided by each of the GBDT implementations. Feature importance reflects the  informativeness of the characteristic for the model. It is computed as the normalized total reduction of the criterion brought by that feature and is also known as the Gini importance. We examine the number of categorical variables in the top 10 most important features for each model and summarize the observations in \cref{tab:feature-importance}.

\begin{table}[htbp]
	\centering
	\resizebox{0.7\linewidth}{!}{
		\begin{tabular}{lrrr}
			\toprule
			\textbf{Model} & \textbf{Russia} & \textbf{Ames} & \textbf{Boston} \\
			\midrule
			CatBoost & 5& 9 & 1 \\
			XGBoost & 1  & 1 & 0 \\
			sklearn & 1 & 0 & 0 \\
			\midrule
			\textbf{Number of categorical features} & 18 & 43 & 1 \\
			\textbf{Total number of features} & 293 & 80 & 13 \\
			\bottomrule
		\end{tabular}
	}
	\caption{Number of categorical features among top 10 most important for the model }
	\label{tab:feature-importance}
\end{table}

As can be seen from \cref{tab:feature-importance}, CatBoost gives clear precedence to the categorical variables whereas other models do not demonstrate any particular preference for them. We assume that this is due to the sophisticated encoding of categorical variables provided by the CatBoost. However, this special treatment does not necessarily boost the performance whereas seriously increasing the training time as we could perceive from \cref{sec:results}.

With respect to the encountered experience of applying CatBoost to the housing price prediction task, we see that it obtains sufficient results when compared to other state-of-the-art models. However, it does not consistently outperform them and one possible reason for this can be its still imperfect optimization for regression problems which would also explain the absence of information regarding its performance on regression tasks. Another prominent problem that we came across is long training time of this model which prevented us from performing more extensive grid search. It is conceivable that with more training resources we could have gained better score for CatBoost as apparently, it incorporates elaborate approaches towards categorical variable processing and building weak estimators which require much more computational power that we had at our disposal. However, it is an open question whether several percent improvements are worth significant training time consuming.


\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\title{Judson's Abstract Algebra: Chapter 1}
\date{}

\begin{document}
\maketitle

\section*{1}

Suppose that

\begin{align*}
A &= \{ x : x \in \mathbb{N} \text{ and } x \text{ is even}\}, \\
B &= \{ x : x \in \mathbb{N} \text{ and } x \text{ is prime}\}, \\
C &= \{ x : x \in \mathbb{N} \text{ and } x \text{ is a multiple of 5}\}.
\end{align*}

Then

\begin{align*}
A \cap B &= \{ 2 \} \\
B \cap C &= \{ 5 \} \\
A \cup B &= \{ x : x \in \mathbb{N} \text{ and } x \text{ is even or } x \text{ is prime}\} \\
A \cap (B \cup C) &= \{ x : x \in \mathbb{N} \text{ and } x \text{ is a multiple of 10}\}
\end{align*}

\section*{2}

If $A = \{ a, b, c \}, B = \{ 1, 2, 3 \}, C = \{ x \}, D = \emptyset$ then

\begin{align*}
A \times B &= \{ (a,1), (a,2), (a,3), (b,1), (b,2), (b,3), (c,1), (c,2), (c,3)\} \\
B \times C &= \{ (1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)\} \\
A \times B \times C &= \{ (a,1,x), (a,2,x), (a,3,x), (b,1,x), (b,2,x), (b,3,x), (c,1,x), (c,2,x), (c,3,x)\} \\
A \times D &= \emptyset.
\end{align*}

\section*{3}

Find an example of two nonempty sets $A$ and $B$ for which $A \times B = B \times A$.

Consider any nonempty set $A = B$.

\section*{4}

Prove $A \cup \emptyset = A$.

\vspace{\baselineskip}

By definition $A \cup \emptyset = \{ x : x \in A \lor x \in \emptyset \}$. Note that the second condition is always false and hence

$$A \cup \emptyset = \{ x : x \in A \lor x \in \emptyset \} = \{ x : x \in A \} = A.$$

Prove $A \cap \emptyset = \emptyset$.

\vspace{\baselineskip}

This is very similar to the previous proof. By definition $A \cap \emptyset = \{ x : x \in A \land x \in \emptyset \}$. Note that the second condition is always false and hence

$$A \cap \emptyset = \{ x : x \in A \land x \in \emptyset \} = \{ \} = \emptyset.$$

\section*{5}

Prove $A \cup B = B \cup A$.

\vspace{\baselineskip}

This follows directly from the definition

$$A \cup B = \{x : x \in A \lor x \in B\} = B \cup A.$$

Prove $A \cap B = B \cap A$.

\vspace{\baselineskip}

This follows directly from the definition

$$A \cap B = \{x : x \in A \land x \in B\} = B \cap A.$$

\section*{6}

Prove $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$.

\vspace{\baselineskip}

Let $x \in A \cup (B \cap C)$ then $x \in A$ or $x \in B \cap C$. If $x \in A$ then $x \in (A \cup B) \cap (A \cup C)$. If $x \in B \cap C$ then $x \in (A \cup B) \cap (A \cup C)$. Hence $A \cup (B \cap C) \subset (A \cup B) \cap (A \cup C)$.

\vspace{\baselineskip}

Let $x \in (A \cup B) \cap (A \cup C)$. Then $x \in A \cup B$ and $x \in A \cup C$. If $x \in A$, then clearly $x \in A \cup (B \cap C)$. If $x \not\in A$ then $x \in B$ and $x \in C$ hence $x \in A \cap B$. From these two facts we have  $(A \cup B) \cap (A \cup C) \subset A \cup (B \cap C)$.

\section*{7}

Prove $A \cap (B \cup C) = (A \cap B) \cup (A \cap C)$.

\vspace{\baselineskip}

Let $x \in A \cap (B \cup C)$. Then $x\in A$ and $x \in B \cup C$. There are two cases. If $x \in B$ then $x \in A \cap B$ by definition. Similarly, if $x \in C$ then $x \in A \cap C$. Hence, by definition, $x \in (A \cap B) \cup (A \cap C)$. This shows $A \cap (B \cup C) \subset (A \cap B) \cup (A \cap C)$.

\vspace{\baselineskip}

Let $x \in (A \cap B) \cup (A \cap C)$. There are two cases. If $x \in A \cap B$ then clearly $x \in A \cap (B \cup C)$. Similarly, if $x \in A \cap C$ then clearly $x \in A \cap (B \cup C)$. This shows $(A \cap B) \cup (A \cap C) \subset A \cap (B \cup C)$. This proves $A \cap (B \cup C) = (A \cap B) \cup (A \cap C)$.

\section*{8}

Prove $A \subset B$ if and only if $A \cap B = A$.

\vspace{\baselineskip}

Assume $A \cap B = A$. Then  for all $a \in A$ it is true that $a \in A \cap B$ and thus $a \in B$. Hence $A \subset B$.

\vspace{\baselineskip}

Assume $A \subset B$. Then for all $a \in A$ it is true that $a \in B$. Since $a \in A$ and $a \in B$ we know $A \subset A \cap B$. Clearly $A \cap B \subset A$, therefore $A \cap B = A$.

\section*{9}

Prove $(A \cap B)' = A' \cup B'$.

\vspace{\baselineskip}

Let $x \in (A \cap B)'$. Then $x \not \in (A \cap B)$. By the definition of intersection either $x \not \in A$ or $x \not \in B$ so $x \in A'$ or $x \in B'$. By definition of union $x \in A' \cup B'$. This shows $(A \cap B)' \subseteq A' \cup B'$.

\vspace{\baselineskip}

Let $x \in A' \cup B'$. Then $x \in A'$ or $x \in B'$ by the definition of union. By the definition of the complement $x \not \in A$ or $x \not \in B$ which implies that $x \not \in A \cap B$. Then, by the definition of the complement, $x \in (A \cap B)'$. This shows $A' \cup B' \subseteq (A \cap B)'$.  Hence $(A \cap B)' = A' \cup B'$.

\section*{10}

\section*{11}

\section*{12}

Prove $(A \cap B) \setminus B = \emptyset$.

\vspace{\baselineskip}

Let $ x \in (A \cap B) \setminus B$. Then $x \in A \cap B$ and $x \not\in B$. This is a contradiction since $x \in A \cap B$ implies $x \in B$. Hence, there are no such $x$. This is what we wanted to prove.

\section*{13}

\section*{14}

\section*{15}

\section*{16}

\section*{17}

\section*{18}

Determine which of the following functions are one-to-one and which are onto. If the function is not onto, determine it's range.

\vspace{\baselineskip}

$f : \mathbb{R} \rightarrow \mathbb{R}$ defined by $f(x) = e^x$ is one-to-one (note that it is a continuous, strictly increasing function), but not onto. It's range is the positive real numbers.

\vspace{\baselineskip}

$f : \mathbb{Z} \rightarrow \mathbb{Z}$ defined by $f(n) = n^2 + 3$ is not one-to-one or onto. Note that $f(-1) = 4 = f(1)$ and that there is no solution to $f(n) = 1$. The range of $f$ is $\{ n^ + 3, n \in \mathbb{N} \}$.

\vspace{\baselineskip}

$f : \mathbb{R} \rightarrow \mathbb{R}$ defined by $f(x) = \sin(x)$ is not one-to-one or onto. Note that $\sin(0) = \sin(2\pi)$. The range of $f$ is $[-1, 1]$.

\vspace{\baselineskip}

$f : \mathbb{Z} \rightarrow \mathbb{Z}$ defined by $f(x) = x^2$ is not one-to-one or onto. Note that $f(-1) = 1 = f(1)$ and that there is no solution to $f(x) = -1$. The range of $f$ is $\{ x^2, x \in \mathbb{N}\}$.

\section*{19}

Let $f: A \rightarrow B$ and $g: B \rightarrow C$ be invertible mappings; that is, mappings such that $f^{-1}$ and $g^{-1}$ exist. Show that $(g \circ f)^{-1} = f^{-1} \circ g^{-1}$.

\vspace{\baselineskip}

Let $c \in C$. Note that there exists $a \in A$ such that

$$(g \circ f)^{-1}(c) = a$$

and

$$(g \circ f)(a) = c.$$

Consider the second equation

\begin{align*}
(g \circ f)(a) &= c \\
g(f(a)) &= c \\
f^{-1}(g^{-1}(g(f(a)))) &= f^{-1}(g^{-1}(c)) \\
a &= f^{-1}(g^{-1}(c)) \\
a &= (f^{-1} \circ g^{-1}) (c).
\end{align*}

Combining this result and the fact that $(g \circ f)^{-1}(c) = a$ yields

$$(g \circ f)^{-1}(c) = a = (f^{-1} \circ g^{-1})(c).$$

This is what we are trying to prove.

\section*{20}

Define a function $f: \mathbb{N} \rightarrow \mathbb{N}$ that is one-to-one but not onto.

\vspace{\baselineskip}

Let $m,n \in \mathbb{N}$. Consider the function $f(x) = 2x$. We know $f$ is one-to-one since

\begin{align*}
f(m) &= f(n) \\
2m &= 2n \\
m &= n
\end{align*}

but notice that $f$ is not onto since there is no solution to f(x) = 3 (or any other odd number).

\vspace{\baselineskip}

Define a function $f: \mathbb{N} \rightarrow \mathbb{N}$ that is onto but not one-to-one.

\vspace{\baselineskip}

Let $m,n \in \mathbb{N}$. Consider the function $f(x) = x - 1$ if $n > 1$ and $f(1) = 1$. Notice that $f$ is onto since $f(n+1) = n$ where $n$ is arbitrary. A counterexample to $f$ being one-to-one is $f(1) = f(2) = 1$.

\section*{21}

Prove the relation defined on $\mathbb{R}^2$ by $(x_1, y_2) \sim (x_2, y_2)$ if $x_1^2 + y_1^2 = x_2^2 + y_2^2$ is an equivalence relation.

\vspace{\baselineskip}

Let $(x_1, y_1), (x_2, y_2), (x_3, y_3) \in \mathbb{R}^2$. First, the relation is trivially reflexive since equality is reflexive. Also, the relation is trivially symmetric since equality is symmetric. To show the relation is transitive assume $(x_1, y_1) \sim (x_2, y_2)$ and $(x_2, y_2) \sim (x_3, y_3)$ and consider

$$x_1^2 + y_1 ^2 = x_2^2 + y_2^2 = x_3^2 + y_3^2.$$

This shows the relation is transitive because of the transitivity of equality.

\section*{26}

Define a relation $\sim$ on $\mathbb{R}^2$ by stating that $(a,b) \sim (c,d)$ if and only if $a^2 + b^2 \leq c^2 + d^2$. Show that $\sim$ is reflexive and transitive, but not symmetric.

\vspace{\baselineskip}

Let $(a,b), (c,d), (x,y) \in \mathbb{R}^2$. Note that $\sim$ is trivially reflexive since $a^2 + b^2 \leq a^2 + b^2$. Assume that $(a,b) \sim (c,d)$ and $(c,d) \sim (x,y)$. It follows that

$$a^2 + b^2 \leq c^2 + d^2 \leq x^2 + y^2$$
$$a^2 + b^2 \leq x^2 + y^2.$$

This shows $\sim$ is transitive. The following counterexample shows $\sim$ is not reflexive. Note $(0,0) \sim (1,1)$ since $0^2 + 0^2 = 0 \leq 2 = 1^2 + 1^2$, but $(1,1) \not \sim (0,0)$ since $2 \not \leq 0$.

\end{document}


%
% body.tex
%
% Copyright © 2020 Libao Jin <jinlibao@outlook.com>
% Distributed under terms of the MIT license.
%
The deadline will be strictly enforced. If you do not submit in time there will be a $20\%$ penalty for each day you're late. If you do not submit in time there will be a $20\%$ penalty upfront plus another $20\%$ for  each day you're late. Remember that you are allowed to work in teams of two on this assignment. You are encouraged to prepare your work in \LaTeX{}; a template will be provided to help you put it all together. If you choose  to submit a hard copy, you may submit only one copy for a team, indicating the names of both contributors. Online submission is encouraged, however, in that case both members of a team should submit the PDF file containing  their work and showing both their names.

\emph{All plots generated in this homework should have a title, legend, and labeled $x$ and $y$-axes.} \\[15pt]

\textbf{Instruction}

\begin{enumerate}[label={\arabic*.}]
  \item Go to \url{https://www.overleaf.com} and sign in (required).
  \item Click \emph{Menu} (up left corner), then \emph{Copy Project}.
  \item Go to \verb|LaTeX/meta.tex| (the file \verb|meta.tex| under the folder \verb|LaTeX|) to change the section and your name, e.g.,
    \begin{itemize}
      % \item change title to \verb|\title{MATH 3340-01 Scientific Computing Homework 6}|
      \item change author to \verb|\author{Albert Einstein \& Carl F. Gauss}|
    \end{itemize}
  \item For Problem 1 and 3, you are encouraged to type solutions in \LaTeX{}. But if you want to write it on the printout, make sure your scanned work is \emph{clear} enough, and compile all solutions \emph{in order}, i.e., 1, 2, 3, in a single PDF (failure to do so will lead to points deduction).
  \item For Problem 2 and 3, you need to write function/script files, store results to output files, and save graphs to figure files. Here are suggested names for function files, script files, output files, and figure files:
    \begin{table}[!hbtp]
      \centering
      % \caption{caption}
      % \label{tab:label}
      \begin{tabular}{cllll}
        \toprule
        Problem & Function File          & Script File     & Output File       & Figure File         \\
        \midrule
        2       & \verb|monteCarlo.m|    & \verb|hw8_p2.m| & \verb|hw8_p2.txt| &                     \\
        3       & \verb|goldenSection.m| & \verb|hw8_p3.m| & \verb|hw8_p3.txt| & \verb|hw8_p3.pdf|   \\
        \bottomrule
      \end{tabular}
    \end{table}

    Once finished, you need to upload these files to the folder \verb|src| on Overleaf. If you have different filenames, please update the filenames in \verb|\lstinputlisting{../src/your_script_name.m}| accordingly. You can code in the provided files in \href{https://libaoj.in/courses/2020s/MATH3340/Homework/8/hw8.zip}{hw8.zip}, and use the MATLAB script \verb|save_results.m| to generate the output files and store the graphs to \verb|.pdf| files automatically (the script filenames should be exactly same as listed above).
  \item Recompile, download and upload the generated PDF to WyoCourses.
  \item You may find \href{https://libaoj.in/files/LaTeX.Mathematical.Symbols.pdf}{\LaTeX{}.Mathematical.Symbols.pdf} and the second part of \href{https://libaoj.in/courses/2020s/MATH3341/slides/Math.3341.Lab.01.Slides.pdf}{Lab 01 Slides} and \href{https://libaoj.in/courses/2020s/MATH3341/slides/Math.3341.Lab.02.Slides.pdf}{Lab 02 Slides} helpful.
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem 1}%
\label{sec:problem_1}
This computation should be done by hand. Use Romberg integration to compute the $R_{3, 3}$ approximation for
\begin{equation}
  \label{eq:p1}
  I = \int_{1}^{3} (x^{3} - 1) e^{-x^{2}} \, dx.
\end{equation}
Perform all calculations by rounding off to four decimal places.
\begin{solution}
  \quad
  % \begin{itemize}
  %   \item $R$ obtained by Romberg integration:
  %     \begin{equation*}
  %       \begin{bmatrix}
  %         R_{11} &        &        \\
  %         R_{21} & R_{22} &        \\
  %         R_{31} & R_{32} & R_{33} \\
  %       \end{bmatrix}
  %       =
  %       \hspace{12cm}                                     % DELETE THIS LINE WHEN YOU TYPE THE ANSWER IN LATEX
  %     \end{equation*}
  %   \item Approximation of \eqref{eq:p1} :
  %     \begin{equation*}
  %       R_{3, 3} =
  %       \hspace{12cm}                                     % DELETE THIS LINE WHEN YOU TYPE THE ANSWER IN LATEX
  %     \end{equation*}
  %   \item All the calculations step by step:
  %     \newpage \quad \vfill                               % DELETE THIS LINE WHEN YOU TYPE THE ANSWER IN LATEX
  % \end{itemize}
  \begin{itemize}
  \item Output file \verb|hw8_p1.txt|:
    \lstinputlisting[style=Plain]{../src/hw8_p1.txt}
  \item Function file \verb|romberg.m|:
    \lstinputlisting[style=MATLAB]{../src/romberg.m}
  \item Script file \verb|hw8_p1.m|:
    \lstinputlisting[style=MATLAB]{../src/hw8_p1.m}
  \end{itemize}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem 2}%
\label{sec:problem_2}
Write a code that integrates the function $f(x, y, z) = 0.7 (x^{2} + y^{2} + z^{2})$ over the unit sphere $S = \{(x, y, z) | x^{2} + y^{2} + z^{2} \leq 1 \}$ using the Monte-Carlo method in three dimensions. Run the code with a sample of $M = 10^{6}$ points; do this ten times and compute the average of the ten results. Then compare this average result with the exact value of the integral, which can be easily calculated analytically. Print the error: the absolute value of the difference between the exact value and the average you got from your ten runs.
\begin{solution}
  \quad
  \begin{itemize}
  \item Output file \verb|hw8_p2.txt|:
    \lstinputlisting[style=Plain]{../src/hw8_p2.txt}
  \item Function file \verb|monteCarlo.m|:
    \lstinputlisting[style=MATLAB]{../src/monteCarlo.m}
  \item Script file \verb|hw8_p2.m|:
    \lstinputlisting[style=MATLAB]{../src/hw8_p2.m}
  \end{itemize}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem 3}%
\label{sec:problem_3}
Write a MATLAB function that implements the golden search method. Use this function to find the minimum of the function $f(x) = \cos(x) - \sin(x)$ on the interval $[1, 3]$ with a tolerance $T = 10^{-7}$. You should  first plot the function and check that it is unimodal on this interval. Also, find the number of iterations needed to locate the value of the minimum with a tolerance of at least $T = 0.1$. Do the latter calculation by hand.
\begin{solution}
  \quad
  \begin{itemize}
  \item The number of iterations needed to locate the value of the minimum with a tolerance of at least $T = 0.1$:
      \quad \vfill                               % DELETE THIS LINE WHEN YOU TYPE THE ANSWER IN LATEX
  \item Output file \verb|hw8_p3.txt|:
    \lstinputlisting[style=Plain]{../src/hw8_p3.txt}
  \item Figure file \verb|hw8_p3.pdf|:
    \begin{figure}[!hbtp]
      \centering
      \includegraphics[width=0.8\linewidth]{../src/hw8_p3.pdf}
      \caption{$f(x)$ is unimodal.}%
      \label{fig:hw8_p3}
    \end{figure}
  \item Function file \verb|goldenSection.m|:
    \lstinputlisting[style=MATLAB]{../src/goldenSection.m}
  \item Script file \verb|hw8_p3.m|:
    \lstinputlisting[style=MATLAB]{../src/hw8_p3.m}
  \end{itemize}
\end{solution}