• Home
  • Textbooks
  • Algorithms
  • Algorithms with numbers

Algorithms

Sanjoy Dasgupta, Christos Papadimitriou, Umesh Vazirani

Chapter 1

Algorithms with numbers - all with Video Answers

Educators

WM

Chapter Questions

02:06

Problem 1

Show that in any base $b \geq 2$, the sum of any three single-digit numbers is at most two digits long.

James Chok
James Chok
Numerade Educator
View

Problem 1

Use the divide-and-conquer integer multiplication algorithm to multiply the two binary integers 10011011 and 10111010.

Nick Johnson
Nick Johnson
Numerade Educator
05:00

Problem 2

Show that any binary integer is at most four times as long as the corresponding decimal integer. For very large numbers, what is the ratio of these two lengths, approximately?

James Chok
James Chok
Numerade Educator
05:14

Problem 2

Show that for any positive integers $n$ and any base $b$, there must some power of $b$ lying in the range $[n, b n]$.

Ibrahima Barry
Ibrahima Barry
Numerade Educator
05:24

Problem 3

A $d$ -ary tree is a rooted tree in which each node has at most $d$ children. Show that any $d$ -ary tree with $n$ nodes must have a depth of $\Omega(\log n / \log d) .$ Can you give a precise formula for the minimum depth it could possibly have?

Chris Trentman
Chris Trentman
Numerade Educator
18:01

Problem 3

Section 2.2 describes a method for solving recurrence relations which is based on analyzing the recursion tree and deriving a formula for the work done at each level. Another (closely related) method is to expand out the recurrence a few times, until a pattern emerges. For instance, let's start with the familiar $T(n)=2 T(n / 2)+O(n) .$ Think of $O(n)$ as being $\leq c n$ for some constant $c$ so: $T(n) \leq 2 T(n / 2)+c n .$ By repeatedly applying this rule, we can bound $T(n)$ in terms of $T(n / 2)$ then $T(n / 4),$ then $T(n / 8),$ and so on, at each step getting closer to the value of $T(\cdot)$ we do know, namely $T(1)=O(1)$.
$$\begin{aligned}
T(n) & \leq 2 T(n / 2)+c n \\
& \leq 2[2 T(n / 4)+c n / 2]+c n=4 T(n / 4)+2 c n \\
& \leq 4[2 T(n / 8)+c n / 4]+2 c n=8 T(n / 8)+3 c n \\
& \leq 8[2 T(n / 16)+c n / 8]+3 c n=16 T(n / 16)+4 c n
\end{aligned}$$
A pattern is emerging... the general term is
\[
T(n) \leq 2^{k} T\left(n / 2^{k}\right)+k c n
\]
Plugging in $k=\log _{2} n,$ we get $T(n) \leq n T(1)+c n \log _{2} n=O(n \log n)$.
(a) Do the same thing for the recurrence $T(n)=3 T(n / 2)+O(n) .$ What is the general $k$ th term in this case? And what value of $k$ should be plugged in to get the answer?
(b) Now try the recurrence $T(n)=T(n-1)+O(1),$ a case which is not covered by the master theorem. Can you solve this too?

Chris Trentman
Chris Trentman
Numerade Educator
04:53

Problem 4

Show that
$$\log (n !)=\Theta(n \log n)$$
(Hint: To show an upper bound, compare $n !$ with $n^{n}$. To show a lower bound, compare it with $(n / 2)^{n / 2}$

WM
William Mead
Numerade Educator
03:17

Problem 4

Suppose you are choosing between the following three algorithms:
• Algorithm A solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time.
• Algorithm B solves problems of size n by recursively solving two subproblems of size n ? 1
and then combining the solutions in constant time.
• Algorithm C solves problems of size n by dividing them into nine subproblems of size n/3,
recursively solving each subproblem, and then combining the solutions in O(n
2
) time.
What are the running times of each of these algorithms (in big-O notation), and which would you
choose?

Bryan Lynn
Bryan Lynn
Numerade Educator
01:06

Problem 5

Unlike a decreasing geometric series, the sum of the harmonic series $1,1 / 2,1 / 3,1 / 4,1 / 5, \ldots$ diverges; that is,
$$\sum_{i=1}^{\infty} \frac{1}{i}=\infty$$
It turns out that, for large $n$, the sum of the first $n$ terms of this series can be well approximated as
$$\sum_{i=1}^{n} \frac{1}{i} \approx \ln n+\gamma$$
where ln is natural logarithm (log base $e=2.718 \ldots$ ) and $\gamma$ is a particular constant $0.57721 \ldots$ Show that
$$\sum_{i=1}^{n} \frac{1}{i}=\Theta(\log n)$$
(Hint: To show an upper bound, decrease each denominator to the next power of two. For a lower bound, increase each denominator to the next power of $2 . .$ )

Nick Johnson
Nick Johnson
Numerade Educator
14:40

Problem 5

Solve the following recurrence relations and give a $\Theta$ bound for each of them.
(a) $T(n)=2 T(n / 3)+1$
(b) $T(n)=5 T(n / 4)+n$
(c) $T(n)=7 T(n / 7)+n$
(d) $T(n)=9 T(n / 3)+n^{2}$
(e) $T^{\prime}(n)=8 T(n / 2)+n^{3}$
(f) $T(n)=49 T(n / 25)+n^{3 / 2} \log n$
$(g) T(n)=T(n-1)+2$
(h) $T(n)=T(n-1)+n^{c},$ where $c \geq 1$ is a constant
(i) $T(n)=T(n-1)+c^{n},$ where $c>1$ is some constant
(j) $T(n)=2 T(n-1)+1$
(k) $T(n)=T(\sqrt{n})+1$

Chris Trentman
Chris Trentman
Numerade Educator
00:04

Problem 6

Prove that the grade-school multiplication algorithm (page 24 ), when applied to binary numbers, always gives the right answer.

Sanchit Gogia
Sanchit Gogia
Numerade Educator
01:39

Problem 6

A linear, time-invariant system has the following impulse response:
(a) Describe in words the effect of this system.
(b) What is the corresponding polynomial?

Arpit Gupta
Arpit Gupta
Numerade Educator
03:49

Problem 7

How long does the recursive multiplication algorithm (page 25 ) take to multiply an $n$ -bit number by an $m$ -bit number? Justify your answer.

Mukesh Devi
Mukesh Devi
Numerade Educator
01:31

Problem 7

What is the sum of the $n$ th roots of unity? What is their product if $n$ is odd? If $n$ is even?

Aayush Gupta
Aayush Gupta
Numerade Educator
07:15

Problem 8

Justify the correctness of the recursive division algorithm given in page $26,$ and show that it takes time $O\left(n^{2}\right)$ on $n$ -bit inputs.

Lucas Gagne
Lucas Gagne
Numerade Educator
02:26

Problem 8

Practice with the fast Fourier transform.
(a) What is the FFT of (1,0,0,0)$?$ What is the appropriate value of $\omega$ in this case? And of which sequence is (1,0,0,0) the FFT?
(b) Repeat for (1,0,1,-1).

Amit Srivastava
Amit Srivastava
Numerade Educator
02:35

Problem 9

Practice with polynomial multiplication by FFT.
(a) Suppose that you want to multiply the two polynomials $x+1$ and $x^{2}+1$ using the FFT. Choose an appropriate power of two, find the FFT of the two sequences, multiply the results componentwise, and compute the inverse FFT to get the final result.
(b) Repeat for the pair of polynomials $1+x+2 x^{2}$ and $2+3 x$.

AG
Ankit Gupta
Numerade Educator
02:51

Problem 9

Starting from the definition of $x \equiv y \bmod N$ (namely, that $N$ divides $x-y$ ), justify the substitution rule
$$x \equiv x^{\prime} \bmod N, y \equiv y^{\prime} \bmod N \Rightarrow x+y \equiv x^{\prime}+y^{\prime} \bmod N$$
and also the corresponding rule for multiplication.

Nicole Krahulik
Nicole Krahulik
Numerade Educator
02:39

Problem 10

Show that if $a=b(\bmod N)$ and if $M$ divides $N$ then $a=b(\bmod M)$

Nick Johnson
Nick Johnson
Numerade Educator
01:00

Problem 10

Find the unique polynomial of degree 4 that takes on values $p(1)=2, p(2)=1, p(3)=0, p(4)=4$ and $p(5)=0 .$ Write your answer in the coefficient representation.

Nick Johnson
Nick Johnson
Numerade Educator
00:42

Problem 11

Is $4^{1536}-9^{4824}$ divisible by $35 ?$

Erika Bustos
Erika Bustos
Numerade Educator
06:38

Problem 11

In justifying our matrix multiplication algorithm (Section 2.5), we claimed the following blockwise property: if $X$ and $Y$ are $n \times n$ matrices, and
\[
X=\left[\begin{array}{ll}
A & B \\
C & D
\end{array}\right], \quad Y=\left[\begin{array}{ll}
E & F \\
G & H
\end{array}\right].
\]
where $A, B, C, D, E, F, G,$ and $H$ are $n / 2 \times n / 2$ submatrices, then the product $X Y$ can be expressed in terms of these blocks:
\[
X Y=\left[\begin{array}{ll}
A & B \\
C & D
\end{array}\right]\left[\begin{array}{ll}
E & F \\
G & H
\end{array}\right]=\left[\begin{array}{ll}
A E+B G & A F+B H \\
C E+D G & C F+D H
\end{array}\right]
\]
Prove this property.

Bryan Lynn
Bryan Lynn
Numerade Educator
01:05

Problem 12

$$\text { What is } 2^{2^{2006}}(\bmod 3) ?$$

Amy Jiang
Amy Jiang
Numerade Educator
04:09

Problem 12

How many lines, as a function of $n$ (in $\Theta(\cdot)$ form), does the following program print? Write a recurrence and solve it. You may assume $n$ is a power of 2.

Bryan Lynn
Bryan Lynn
Numerade Educator
00:56

Problem 13

Is the difference of $5^{30,000}$ and $6^{123,456}$ a multiple of $31 ?$

Susan Cooper
Susan Cooper
Numerade Educator
06:01

Problem 13

A binary tree is $f u l l$ if all of its vertices have either zero or two children. Let $B_{n}$ denote the number of full binary trees with $n$ vertices.
(a) By drawing out all full binary trees with $3,5,$ or 7 vertices, determine the exact values of $B_{3}, B_{5},$ and $B_{7},$ Why have we left out even numbers of vertices, like $B_{4} ?$
(b) For general $n,$ derive a recurrence relation for $B_{n^{*}}$
(c) Show by induction that $B_{n}$ is $\Omega\left(2^{n}\right)$.

Chris Trentman
Chris Trentman
Numerade Educator
04:40

Problem 14

Suppose you want to compute the $n$ th Fibonacci number $F_{n},$ modulo an integer $p$. Can you find an efficient way to do this? (Hint: Recall Exercise 0.4.)

Runpeng Li
Runpeng Li
Numerade Educator
01:28

Problem 14

You are given an array of $n$ elements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Show how to remove all duplicates from the array in time $O(n \log n)$.

James Kiss
James Kiss
Numerade Educator
05:18

Problem 15

In our median-finding algorithm (Section 2.4 ), a basic primitive is the split operation, which takes as input an array $S$ and a value $v$ and then divides $S$ into three sets: the elements less than $v,$ the elements equal to $v,$ and the elements greater than $v$. Show how to implement this split operation in place, that is, without allocating new memory.

Bryan Lynn
Bryan Lynn
Numerade Educator
01:17

Problem 15

Determine necessary and sufficient conditions on $x$ and $c$ so that the following holds: for any $a, b$ if $a x \equiv b x \bmod c,$ then $a \equiv b \bmod c$

Sriram Soundarrajan
Sriram Soundarrajan
Numerade Educator
01:56

Problem 16

The algorithm for computing $a^{b}$ mod $c$ by repeated squaring does not necessarily lead to the minimum number of multiplications. Give an example of $b>10$ where the exponentiation can be performed using fewer multiplications, by some other method.

James Kiss
James Kiss
Numerade Educator
01:39

Problem 16

You are given an infinite array $A[\cdot]$ in which the first $n$ cells contain integers in sorted order and the rest of the cells are filled with $\infty .$ You are not given the value of $n .$ Describe an algorithm that takes an integer $x$ as input and finds a position in the array containing $x,$ if such a position exists, in $O(\log n)$ time. (If you are disturbed by the fact that the array $A$ has infinite length, assume instead that it is of length $n,$ but that you don't know this length, and that the implementation of the array data type in your programming language returns the error message oo whenever elements $A[i]$ with $i>n$ are accessed.

Manik Pulyani
Manik Pulyani
Numerade Educator
03:39

Problem 17

Consider the problem of computing $x^{y}$ for given integers $x$ and $y:$ we want the whole answer, not modulo a third integer. We know two algorithms for doing this: the iterative algorithm which performs $y-1$ multiplications by $x ;$ and the recursive algorithm based on the binary expansion of $y$ Compare the time requirements of these two algorithms, assuming that the time to multiply an $n$ -bit number by an $m$ -bit number is $O(m n)$

Lucas Gagne
Lucas Gagne
Numerade Educator
03:19

Problem 17

Given a sorted array of distinct integers $A[1, \ldots, n],$ you want to find out whether there is an index $i$ for which $A[i]=i .$ Give a divide-and-conquer algorithm that runs in time $O(\log n)$.

Bryan Lynn
Bryan Lynn
Numerade Educator
08:02

Problem 18

Compute $\operatorname{gcd}(210,588)$ two different ways: by finding the factorization of each number, and by using Euclid's algorithm.

Bryan Lynn
Bryan Lynn
Numerade Educator
02:09

Problem 18

Consider the task of searching a sorted array $A[1 \ldots n]$ for a given element $x:$ a task we usually perform by binary search in time $O(\log n)$. Show that any algorithm that accesses the array only via comparisons (that is, by asking questions of the form "is $A[i] \leq z ?$ ), must take $\Omega(\log n)$ steps.

James Kiss
James Kiss
Numerade Educator
01:05

Problem 19

The Fibonacci numbers $F_{0}, F_{1}, \ldots$ are given by the recurrence $F_{n+1}=F_{n}+F_{n-1}, F_{0}=0, F_{1}=1$ Show that for any $n \geq 1, \operatorname{gcd}\left(F_{n+1}, F_{n}\right)=1$

Nick Johnson
Nick Johnson
Numerade Educator
05:29

Problem 19

A k-way merge operation. Suppose you have $k$ sorted arrays, each with $n$ elements, and you want to combine them into a single sorted array of $k n$ elements.
(a) Here's one strategy: Using the merge procedure from Section $2.3,$ merge the first two arrays, then merge in the third, then merge in the fourth, and so on. What is the time complexity of this algorithm, in terms of $k$ and $n ?$
(b) Give a more efficient solution to this problem, using divide-and-conquer.

Narayan Hari
Narayan Hari
Numerade Educator
00:25

Problem 20

Find the inverse of: $20 \mathrm{mod} 79,3 \mathrm{mod} 62,21 \mathrm{mod} 91,5 \mathrm{mod} 23$

Heather Zimmers
Heather Zimmers
Numerade Educator
01:28

Problem 20

Show that any array of integers $x[1 \ldots . n]$ can be sorted in $O(n+M)$ time, where
\[
M=\max _{i} x_{i}-\min _{i} x_{i}
\]
For small $M,$ this is linear time: why doesn't the $\Omega(n \log n)$ lower bound apply in this case?

James Kiss
James Kiss
Numerade Educator
05:19

Problem 21

Mean and median. One of the most basic tasks in statistics is to summarize a set of observations
$\left\{x_{1}, x_{2}, \ldots, x_{n}\right\} \subseteq \mathbb{R}$ by a single number. Two popular choices for this summary statistic are:
'The median, which we'll call $\mu_{1}$
The mean, which we'll call $\mu_{2}$
(a) Show that the median is the value of $\mu$ that minimizes the function
\[
\sum_{i}\left|x_{i}-\mu\right|.
\]
You can assume for simplicity that $n$ is odd. (Hint: Show that for any $\mu \neq \mu_{1},$ the function decreases if you move $\mu$ either slightly to the left or slightly to the right.)
(b) Show that the mean is the value of $\mu$ that minimizes the function
\[
\sum_{i}\left(x_{i}-\mu\right)^{2}
\]
One way to do this is by calculus. Another method is to prove that for any $\mu \in \mathbb{R}$,
\[
\sum_{i}\left(x_{i}-\mu\right)^{2}=\sum_{i}\left(x_{i}-\mu_{2}\right)^{2}+n\left(\mu-\mu_{2}\right)^{2}
\].
Notice how the function for $\mu_{2}$ penalizes points that are far from $\mu$ much more heavily than the function for $\mu_{1} .$ Thus $\mu_{2}$ tries much harder to be close to all the observations. This might sound like a good thing at some level, but it is statistically undesirable because just a few outliers can severely throw off the estimate of $\mu_{2}$. It is therefore sometimes said that $\mu_{1}$ is a more robust estimator than $\mu_{2}$. Worse than either of them, however, is $\mu_{\infty},$ the value of $\mu$ that minimizes the function
\[
\max _{i}\left|x_{i}-\mu\right|
\]
(c) Show that $\mu_{\infty}$ can be computed in $O(n)$ time (assuming the numbers $x_{i}$ are small enough that basic arithmetic operations on them take unit time).

Harsh Gadhiya
Harsh Gadhiya
Numerade Educator
01:48

Problem 21

How many integers modulo $11^{3}$ have inverses? (Note: $11^{3}=1331 .$ )

James Chok
James Chok
Numerade Educator
02:33

Problem 22

Prove or disprove: If $a$ has an inverse modulo $b,$ then $b$ has an inverse modulo $a$.

James Chok
James Chok
Numerade Educator
02:09

Problem 22

You are given two sorted lists of size $m$ and $n .$ Give an $O(\log m+\log n)$ time algorithm for computing the $k$ th smallest element in the union of the two lists.

James Kiss
James Kiss
Numerade Educator
02:33

Problem 23

Show that if $a$ has a multiplicative inverse modulo $N,$ then this inverse is unique (modulo $N$ ).

James Chok
James Chok
Numerade Educator
03:06

Problem 23

An array $A[1 \ldots . n]$ is said to have a majority element if more than half of its entries are the same. Given an array, the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The elements of the array are not necessarily from some ordered domain like the integers, and so there can be no comparisons of the form "is $A[i]>A[j] ? "$ (Think of the array elements as GIF files, say.) However you can answer questions of the form: "is $A[i]=A[j] ?$ in constant time.
(a) Show how to solve this problem in $O(n \log n)$ time. (Hint: Split the array $A$ into two arrays $A_{1}$ and $A_{2}$ of half the size. Does knowing the majority elements of $A_{1}$ and $A_{2}$ help you figure out the majority element of $A$ ? If $s o,$ you can use a divide-and-conquer approach.)
(b) Can you give a linear-time algorithm? (Hint: Here's another divide-and-conquer approach:
Pair up the elements of $A$ arbitrarily, to get $n / 2$ pairs
Look at each pair: if the two elements are different, discard both of them; if they are the same, keep just one of them
Show that after this procedure there are at most $n / 2$ elements left, and that they have a majority element if and only if $A$ does.

Clarissa Noh
Clarissa Noh
Numerade Educator
01:18

Problem 24

If $p$ is prime, how many elements of $\left\{0,1, \ldots, p^{n}-1\right\}$ have an inverse modulo $p^{n} ?$

James Chok
James Chok
Numerade Educator
02:24

Problem 24

On page 66 there is a high-level description of the quicksort algorithm.
(a) Write down the pseudocode for quicksort.
(b) Show that its worst-case running time on an array of size $n$ is $\Theta\left(n^{2}\right)$
(c) Show that its expected running time satisfies the recurrence relation
\[
T(n) \leq O(n)+\frac{1}{n} \sum_{i=1}^{n-1}(T(i)+T(n-i))
\]
Then, show that the solution to this recurrence is $O(n \log n)$.

James Kiss
James Kiss
Numerade Educator
00:45

Problem 25

Calculate $2^{125}$ mod 127 using any method you choose. (Hint: 127 is prime.)

Katherine Mcgovern
Katherine Mcgovern
Numerade Educator
01:40

Problem 25

In Section 2.1 we described an algorithm that multiplies two $n$ -bit binary integers $x$ and $y$ in time $n^{a},$ where $a=\log _{2} 3 .$ Call this procedure fastmultiply $(x, y)$.
(a) We want to convert the decimal integer $10^{n}$ (a 1 followed by $n$ zeros) into binary. Here is the algorithm (assume $n$ is a power of 2 ):
Fill in the missing details. Then give a recurrence relation for the running time of the algorithm, and solve the recurrence.
(b) Next, we want to convert any decimal integer $x$ with $n$ digits (where $n$ is a power of 2 ) into binary. The algorithm is the following:
Here binary[.] is a vector that contains the binary representation of all one-digit integers. That is, binary[0] $=0_{2},$ binary $[1]=1_{2},$ up to binary $[9]=1001_{2} .$ Assume that a lookup in binary takes $O(1)$ time. Fill in the missing details. Once again, give a recurrence for the running time of the algorithm, and solve it.

Manik Pulyani
Manik Pulyani
Numerade Educator
01:26

Problem 26

What is the least significant decimal digit of $17^{17^{17}} ?(\text {Hint: For distinct primes } p, q, \text { and any } a \neq 0$ $\left.(\bmod p q), \text { we proved the formula } a^{(p-1)(q-1)} \equiv 1(\bmod p q) \text { in Section } 1.4 .2 .\right)$

Nick Johnson
Nick Johnson
Numerade Educator
02:12

Problem 26

Professor $\mathrm{F}$. Lake tells his class that it is asymptotically faster to square an $n$ -bit integer than to multiply two $n$ -bit integers. Should they believe him?

Gaurav Kalra
Gaurav Kalra
Numerade Educator
04:58

Problem 27

Consider an RSA key set with $p=17, q=23, N=391,$ and $e=3$ (as in Figure 1.9 ). What value of $d$ should be used for the secret key? What is the encryption of the message $M=41 ?$

Bryan Lynn
Bryan Lynn
Numerade Educator
05:55

Problem 27

The square of a matrix $A$ is its product with itself, $A A$
(a) Show that five multiplications are sufficient to compute the square of a $2 \times 2$ matrix.
(b) What is wrong with the following algorithm for computing the square of an $n \times n$ matrix? "Use a divide-and-conquer approach as in Strassen's algorithm, except that in stead of getting 7 subproblems of size $n / 2,$ we now get 5 subproblems of size $n / 2$ thanks to part (a). Using the same analysis as in Strassen's algorithm, we can conclude that the algorithm runs in time $O\left(n^{\log _{2} 5}\right) . "$
(c) In fact, squaring matrices is no easier than matrix multiplication. In this part, you will show that if $n \times n$ matrices can be squared in time $S(n)=O\left(n^{c}\right),$ then any two $n \times n$ matrices can be multiplied in time $O\left(n^{c}\right)$
i. Given two $n \times n$ matrices $A$ and $B$, show that the matrix $A B+B A$ can be computed in
\[
\text { time } 3 S(n)+O\left(n^{2}\right)
\]
ii. Given two $n \times n$ matrices $X$ and $Y$, define the $2 n \times 2 n$ matrices $A$ and $B$ as follows:
\[
A=\left[\begin{array}{ll}
X & 0 \\
0 & 0
\end{array}\right] \text { and } B=\left[\begin{array}{ll}
0 & Y \\
0 & 0
\end{array}\right]
\]
What is $A B+B A,$ in terms of $X$ and $Y ?$
iii. Using (i) and (ii), argue that the product $X Y$ can be computed in time $3 S(2 n)+O\left(n^{2}\right)$ .Conclude that matrix multiplication takes time $O\left(n^{c}\right)$.

Gabriel Eduok
Gabriel Eduok
Numerade Educator
02:15

Problem 28

In an RSA cryptosystem, $p=7$ and $q=11$ (as in Figure 1.9 ). Find appropriate exponents $d$ and $c$

James Kiss
James Kiss
Numerade Educator
06:38

Problem 28

The Hadamard matrices H0, H1, H2, . . . are defined as follows:
• H0 is the 1 × 1 matrix
1

• For k > 0, Hk is the 2
k × 2
k matrix
Hk =

Hk?1 Hk?1
Hk?1 ?Hk?1

Show that if v is a column vector of length n = 2
k
, then the matrix-vector product Hkv can be
calculated using O(n log n) operations. Assume that all the numbers involved are small enough
that basic arithmetic operations like addition and multiplication take unit time.

Bryan Lynn
Bryan Lynn
Numerade Educator
00:36

Problem 29

Let $[m]$ denote the set $\{0,1, \ldots, m-1\} .$ For each of the following families of hash functions, say whether or not it is universal, and determine how many random bits are needed to choose a function from the family.
(a) $H=\left\{h_{a_{1}, a_{2}}: a_{1}, a_{2} \in[m]\right\},$ where $m$ is a fixed prime and
$$h_{a_{1}, a_{2}}\left(x_{1}, x_{2}\right)=a_{1} x_{1}+a_{2} x_{2} \bmod m$$
Notice that each of these functions has signature $h_{a_{1}, a_{2}}:[m]^{2} \rightarrow[m],$ that is, it maps a pair of integers in $[\mathrm{m}]$ to a single integer in $[\mathrm{m}]$
(b) $H$ is as before, except that now $m=2^{k}$ is some fixed power of 2
(c) $H$ is the set of all functions $f:[m] \rightarrow[m-1]$

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
02:52

Problem 29

Suppose we want to evaluate the polynomial p(x) = a0 + a1x + a2x
2 + · · · + anx
n at point x.
(a) Show that the following simple routine, known as Horner’s rule, does the job and leaves the
answer in z.
z = an
for i = n ? 1 downto 0:
z = zx + ai
(b) How many additions and multiplications does this routine use, as a function of n? Can you
find a polynomial for which an alternative method is substantially better?

Clarissa Noh
Clarissa Noh
Numerade Educator
05:55

Problem 30

The grade-school algorithm for multiplying two $n$ -bit binary numbers $x$ and $y$ consists of adding together $n$ copies of $x,$ each appropriately left-shifted. Each copy, when shifted, is at most $2 n$ bits long.

In this problem, we will examine a scheme for adding $n$ binary numbers, each $m$ bits long, using a circuit or a parallel architecture. The main parameter of interest in this question is therefore the depth of the circuit or the longest path from the input to the output of the circuit. This determines the total time taken for computing the function. To add two $m$ -bit binary numbers naively, we must wait for the carry bit from position $i-1$ before we can figure out the $i$ th bit of the answer. This leads to a circuit of depth $O(m) .$ However carry lookahead circuits (see wikipedia.com if you want to know more about this) can add in $O(\log m)$ depth.
(a) Assuming you have carry lookahead circuits for addition, show how to add $n$ numbers each $m$ bits long using a circuit of depth $O((\log n)(\log m))$
(b) When adding three $m$ -bit binary numbers $x+y+z,$ there is a trick we can use to parallelize the process. Instead of carrying out the addition completely, we can re-express the result as the sum of just $t w o$ binary numbers $r+s,$ such that the $i$ th bits of $r$ and $s$ can be computed independently of the other bits, Show how this can be done (Hint:Qne of the numbers

Gabriel Eduok
Gabriel Eduok
Numerade Educator
02:25

Problem 30

This problem illustrates how to do the Fourier Transform (FT) in modular arithmetic, for example, modulo 7.
(a) There is a number $\omega$ such that all the powers $\omega, \omega^{2}, \ldots, \omega^{6}$ are distinct (modulo 7 ). Find this $\omega,$ and show that $\omega+\omega^{2}+\dots+\omega^{6}=0 .$ (Interestingly, for any prime modulus there is such a number.)
(b) Using the matrix form of the FT, produce the transform of the sequence (0,1,1,1,5,2) modulo $7 ;$ that is, multiply this vector by the matrix $M_{6}(\omega),$ for the value of $\omega$ you found earlier. In the matrix multiplication, all calculations should be performed modulo 7 .
(c) Write down the matrix necessary to perform the inverse FT. Show that multiplying by this matrix returns the original sequence. (Again all arithmetic should be performed modulo $7 .$ )
(d) Now show how to multiply the polynomials $x^{2}+x+1$ and $x^{3}+2 x-1$ using the FT modulo 7.

Amit Srivastava
Amit Srivastava
Numerade Educator
02:24

Problem 31

Consider the problem of computing $N !=1 \cdot 2 \cdot 3 \cdots N$
(a) If $N$ is an $n$ -bit number, how many bits long is $N !,$ approximately (in $\Theta(\cdot)$ form)?
(b) Give an algorithm to compute $N !$ and analyze its running time.

James Kiss
James Kiss
Numerade Educator
08:02

Problem 31

In Section $1.2 .3,$ we studied Euclid's algorithm for computing the greatest common divisor (ged) of two positive integers: the largest integer which divides them both. Here we will look at an alternative algorithm based on divide-and-conquer.
(a) Show that the following rule is true.
\[
\operatorname{gcd}(a, b)=\left\{\begin{array}{ll}
2 \operatorname{gcd}(a / 2, b / 2) & \text { if } a, b \text { are even } \\
\operatorname{gcd}(a, b / 2) & \text { if } a \text { is odd }, b \text { is even } \\
\operatorname{gcd}((a-b) / 2, b) & \text { if } a, b \text { are odd }
\end{array}\right.
\]
(b) Give an efficient divide-and-conquer algorithm for greatest common divisor.
(c) How does the efficiency of your algorithm compare to Euclid's algorithm if $a$ and $b$ are $n$ -bit integers? (In particular, since $n$ might be large you cannot assume that basic arithmetic operations like addition take constant time.)

Bryan Lynn
Bryan Lynn
Numerade Educator
02:13

Problem 32

A positive integer $N$ is a power if it is of the form $q^{k},$ where $q, k$ are positive integers and $k>1$
(a) Give an efficient algorithm that takes as input a number $N$ and determines whether it is a square, that is, whether it can be written as $q^{2}$ for some positive integer $q$. What is the running time of your algorithm?
(b) Show that if $N=q^{k}$ (with $N, q,$ and $k$ all positive integers), then either $k \leq \log N$ or $N=1$
(c) Give an efficient algorithm for determining whether a positive integer $N$ is a power. Analyze its running time.

Lucas Gagne
Lucas Gagne
Numerade Educator
51:43

Problem 32

In this problem we will develop a divide-and-conquer algorithm for the following geometric task.
CLOSEST PAIR
Input: A set of points in the plane, $\left\{p_{1}=\left(x_{1}, y_{1}\right), p_{2}=\left(x_{2}, y_{2}\right), \ldots, p_{n}=\left(x_{n}, y_{n}\right)\right\}$
Output: The closest pair of points: that is, the pair $p_{i} \neq p_{j}$ for which the distance between $p_{i}$ and $p_{j},$ that is,
\[
\sqrt{\left(x_{i}-x_{j}\right)^{2}+\left(y_{i}-y_{j}\right)^{2}}
\]
is minimized.
For simplicity, assume that n is a power of two, and that all the x-coordinates xi are distinct, as
are the y-coordinates.
Here’s a high-level overview of the algorithm:
• Find a value x for which exactly half the points have xi < x, and half have xi > x. On this
basis, split the points into two groups, L and R.
• Recursively find the closest pair in L and in R. Say these pairs are pL, qL ? L and pR, qR ? R,
with distances dL and dR respectively. Let d be the smaller of these two distances.
• It remains to be seen whether there is a point in L and a point in R that are less than
distance d apart from each other. To this end, discard all points with xi < x?d or xi > x+d
and sort the remaining points by y-coordinate.
• Now, go through this sorted list, and for each point, compute its distance to the seven subsequent points in the list. Let pM , qM be the closest pair found in this way.
• The answer is one of the three pairs {pL, qL}, {pR, qR}, {pM, qM }, whichever is closest.
(a) In order to prove the correctness of this algorithm, start by showing the following property:
any square of size d × d in the plane contains at most four points of L.
(b) Now show that the algorithm is correct. The only case which needs careful consideration is
when the closest pair is split between L and R.
(c) Write down the pseudocode for the algorithm, and show that its running time is given by
the recurrence:
T(n) = 2T(n/2) + O(n log n).
Show that the solution to this recurrence is O(n log2
n).
(d) Can you bring the running time down to O(n log n)?

Oswaldo Jiménez
Oswaldo Jiménez
Numerade Educator
02:40

Problem 33

Give an efficient algorithm to compute the least common multiple of two $n$ -bit numbers $x$ and $y,$ that is, the smallest number divisible by both $x$ and $y .$ What is the running time of your algorithm as a function of $n ?$

Bryan Lynn
Bryan Lynn
Numerade Educator
01:10

Problem 34

On page $38,$ we claimed that since about a $1 / n$ fraction of $n$ -bit numbers are prime, on average it is sufficient to draw $O(n)$ random $n$ -bit numbers before hitting a prime. We now justify this rigorously.
Suppose a particular coin has a probability $p$ of coming up heads. How many times must you toss it, on average, before it comes up heads? (Hint: Method 1: start by showing that the correct expression is $\sum_{i=1}^{\infty} i(1-p)^{i-1} p .$ Method 2: if $E$ is the average number of coin tosses, show that $E=1+(1-p) E .)$

Stephen Hobbs
Stephen Hobbs
Numerade Educator
07:58

Problem 35

Wilson's theorem says that a number $N$ is prime if and only if $$(N-1) ! \equiv-1 \quad(\bmod N)$$
(a) If $p$ is prime, then we know every number $1 \leq x<p$ is invertible modulo $p$. Which of these numbers are their own inverse?
(b) By pairing up multiplicative inverses, show that $(p-1) ! \equiv-1(\bmod p)$ for prime $p$
(c) Show that if $N$ is not prime, then $(N-1) ! \neq-1(\bmod N) .$ (Hint: Consider $d=\operatorname{gcd}(N,(N-$
1)!
(d) Unlike Fermat's Little theorem, Wilson's theorem is an if-and-only-if condition for primality. Why can't we immediately base a primality test on this rule?

Trang Hoang
Trang Hoang
Numerade Educator
02:15

Problem 36

Square roots. In this problem, we'll see that it is easy to compute square roots modulo a prime $p$ with $p \equiv 3(\bmod 4)$
(a) Suppose $p \equiv 3(\bmod 4) .$ Show that $(p+1) / 4$ is an integer.
(b) We say $x$ is a square root of $a$ modulo $p$ if $a \equiv x^{2}(\bmod p) .$ Show that if $p \equiv 3(\bmod 4)$ and if $a$ has a square root modulo $p,$ then $a^{(p+1) / 4}$ is such a square root.

James Chok
James Chok
Numerade Educator
01:49

Problem 37

The Chinese remainder theorem.
(a) Make a table with three columns. The first column is all numbers from 0 to 14. The second is the residues of these numbers modulo $3 ;$ the third column is the residues modulo $5 .$ What do you observe?
(b) Prove that if $p$ and $q$ are distinct primes, then for every pair $(j, k)$ with $0 \leq j<p$ and $0 \leq k<q,$ there is a unique integer $0 \leq i<p q$ such that $i \equiv j \bmod p$ and $i=k \bmod q .$ (Hint:
Prove that no two different $i$ 's in this range can have the same $(j, k),$ and then count.
(c) In this one-to-one correspondence between integers and pairs, it is easy to go from $i$ to $(j, k)$ Prove that the following formula takes you the other way:
\[
i=\left\{j \cdot q \cdot\left(q^{1} \bmod p\right)+k \cdot p \cdot\left(p^{-1} \bmod q\right)\right\} \bmod p q
\]
(d) Can you generalize parts (b) and (c) to more than two primes?

Trang Hoang
Trang Hoang
Numerade Educator
06:21

Problem 38

To see if a number, say $562437487,$ is divisible by $3,$ you just add up the digits of its decimal representation, and see if the result is divisible by
$3 .(5+6+2+4+3+7+4+8+7=46,$ so it
is not divisible by $3 .)$
To see if the same number is divisible by $11,$ you can do this: subdivide the number into pairs of digits, from the right-hand end $(87,74,43,62,5),$ add these numbers, and see if the sum is divisible by 11 (if it's too big, repeat).

How about $37 ?$ To see if the number is divisible by $37,$ subdivide it into triples from the end (487,437,562) add these up, and see if the sum is divisible by 37

This is true for any prime $p$ other than 2 and $5 .$ That is, for any prime $p \neq 2,5,$ there is an integer
$r$ such that in order to see if $p$ divides a decimal number $n,$ we break $n$ into $r$ -tuples of decimal digits (starting from the right-hand end), add up these $r$ -tuples, and check if the sum is divisible by $p$
(a) What is the smallest such $r$ for $p=13 ?$ For $p=17 ?$
(b) Show that $r$ is a divisor of $p-1$

Eric Mockensturm
Eric Mockensturm
Numerade Educator
03:11

Problem 39

Give a polynomial-time algorithm for computing $a^{b^{c}}$ mod $p$, given $a, b, c,$ and prime $p$

Ahmad Reda
Ahmad Reda
Numerade Educator
02:15

Problem 40

Show that if $x$ is a nontrivial square root of 1 modulo $N,$ that is, if $x^{2} \equiv 1 \bmod N$ but $x \neq$ ±1 mod $N,$ then $N$ must be composite. (For instance, $4^{2} \equiv 1$ mod 15 but $4 \neq\pm 1$ mod $15 ;$ thus 4 is a nontrivial square root of 1 modulo $15 .$ )

James Chok
James Chok
Numerade Educator
01:18

Problem 41

Quadratic residues. Fix a positive integer $N .$ We say that $a$ is a quadratic residue modulo $N$ if there exists $x$ such that $a=x^{2} \bmod N$
(a) Let $N$ be an odd prime and $a$ be a non-zero quadratic residue modulo $N$. Show that there are exactly two values in $\{0,1, \ldots, N-1\}$ satisfying $x^{2} \equiv a \bmod N$
(b) Show that if $N$ is an odd prime, there are exactly $(N+1) / 2$ quadratic residues in $\{0,1, \ldots, N-$ 1\}
(c) Give an example of positive integers $a$ and $N$ such that $x^{2} \equiv a \bmod N$ has more than two solutions in $\{0,1, \ldots, N-1\}$

James Chok
James Chok
Numerade Educator
01:18

Problem 42

Suppose that instead of using a composite $N=p q$ in the RSA cryptosystem (Figure 1.9 ), we simply use a prime modulus $p$. As in RSA, we would have an encryption exponent $e,$ and the encryption of a message $m$ mod $p$ would be $m^{e}$ mod $p$. Prove that this new cryptosystem is not secure, by giving an efficient algorithm to decrypt: that is, an algorithm that given $p, e,$ and $m^{e} \bmod p$ as input, computes $m$ mod $p$. Justify the correctness and analyze the running time of your decryption algorithm.

James Chok
James Chok
Numerade Educator
03:17

Problem 43

In the RSA cryptosystem, Alice's public key $(N, c)$ is available to everyone. Suppose that her private key $d$ is compromised and becomes known to Eve. Show that if $e=3$ (a common choice) then Eve can efficiently factor $N$

Anand Jangid
Anand Jangid
Numerade Educator
05:16

Problem 44

Alice and her three friends are all users of the RSA cryptosystem. Her friends have public keys $\left(N_{i}, e_{i}=3\right), i=1,2,3,$ where as always, $N_{i}=p_{i} q_{i}$ for randomly chosen $n$ -bit primes $p_{i}, q_{i} .$ Show that if Alice sends the same $n$ -bit message $M$ (encrypted using RSA) to each of her friends, then anyone who intercepts all three encrypted messages will be able to efficiently recover $M$ (Hint: It helps to have solved problem 1.37 first.)

Bryan Lynn
Bryan Lynn
Numerade Educator
06:05

Problem 45

RSA and digital signatures. Recall that in the RSA public-key cryptosystem, each user has a public key $P=(N, e)$ and a secret key $d$. In a digital signature scheme, there are two algorithms, $\operatorname{sign}$ and verify. The sign procedure takes a message and a secret key, then outputs a signature $\sigma .$ The verify procedure takes a public key $(N, e),$ a signature $\sigma,$ and a message $M,$ then returns "true" if $\sigma$ could have been created by sign (when called with message $M$ and the secret key corresponding to the public key $(N, e)$ ); "false" otherwise.
(a) Why would we want digital signatures?
(b) An RSA signature consists of $\operatorname{sign}(M, d)=M^{d}(\bmod N),$ where $d$ is a secret key and $N$ is part of the public key. Show that anyone who knows the public key $(N, e)$ can perform verify $\left((N, c), M^{d}, M\right),$ i.e., they can check that a signature really was created by the private key. Give an implementation and prove its correctness.
(c) Generate your own RSA modulus $N=p q$, public key $e$, and private key $d$ (you don't need to use a computer). Pick $p$ and $q$ so you have a 4 -digit modulus and work by hand. Now sign your name using the private exponent of this RSA modulus. To do this you will need to specify some one-to-one mapping from strings to integers in $[0, N-1] .$ Specify any mapping you like. Give the mapping from your name to numbers $m_{1}, m_{2}, \ldots, m_{k},$ then sign the first number by giving the value $m_{1}^{d}(\bmod N),$ and finally show that $\left(m_{1}^{d}\right)^{e}=m_{1}(\bmod N)$
(d) Alice wants to write a message that looks like it was digitally signed by Bob. She notices that Bob's public RSA key is $(17,391) .$ To what exponent should she raise her message?

David Mccaslin
David Mccaslin
Numerade Educator
05:16

Problem 46

Digital signatures, continued. Consider the signature scheme of Exercise 1.45
(a) Signing involves decryption, and is therefore risky. Show that if Bob agrees to sign anything he is asked to, Eve can take advantage of this and decrypt any message sent by Alice to Bob.
(b) Suppose that Bob is more careful, and refuses to sign messages if their signatures look suspiciously like text. (We assume that a randomly chosen message-that is, a random number in the range $\{1, \ldots, N-1\}-$ is very unlikely to look like text.) Describe a way in which Eve can nevertheless still decrypt messages from Alice to Bob, by getting Bob to sign messages whose signatures look random.

Bryan Lynn
Bryan Lynn
Numerade Educator