Mad 3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Full-Name: Ha Khai Hoan

ID card: QE170157
Tel.: 0356295910
Class: AI17C
Email: hoanhkqe170157@fpt.edu.vn

Answers:
Page 178:
13. List all the steps used to search for 9 in the sequence 1,
3, 4, 5, 6, 8, 9, 11 using
a) a linear search.
Note that there are eight numbers, so n = 8.
i = 1, 1 6 ≠ 9
i = 2, 3 6 ≠ 9
i = 3, 4 6 ≠ 9
i = 4, 5 6 ≠ 9
i = 5, 6 6 ≠ 9
i = 6, 8 6 ≠ 9
i = 7, 9 ≠ 9
Stop because 9 was found at i = 7
b) a binary search.
Start with 1, 3, 4, 5, 6, 8, 9, 11 (i = 1, j = 8, m = 4) Compare the
m-th element in the list with 9. In this case m = 4, so compare 9
with 5.
Since 5 < 9, take the second half 6, 8, 9, 11 (i = 5, j = 8, m = 6)
Compare the m-th element in the list with 9. In this case m = 6,
so compare 9 with 8.
Since 8 < 9, take the second half 9, 11 (i = 7, j = 8, m = 7)
Compare the m-th element in the list with 9. In this case m = 7,
so compare 9 with 9.
Since 9 is not less than 9, take the first half by setting j = 7(= m).
At this moment the algorithm exits the while loop because i is
not less than j.
After exiting the loop, compare 9 with the ith element. Since 9 =
9, the algorithms returns i, which is 7.

24. Describe an algorithm that determines whether a function


from a finite set to another finite set is one-to-one
The algorithm is
Procedure one-to-one (a1,a2,…,an: with n ≥ 1,b1,b2,..,bm: integers
with m ≥ 1: function)
Truth := true
For i := 1 to n
For j := to n
If (ai aj and f(a≠ai) = f(aj) then truth := false
Return truth

Page 191:
6.
To satisfy the definition of O(x2 ), we need to find an
appropriate choice of c and k. Let’s consider x ≥ 1000 and
reason by inequalities:

2 3
x +2 x x +2 x 1 1 3
2 x +1
< 2x
= 2 x2 + 1 < 2 x2 + x 2 = 2 x2

2
x +2 x 3
Since 2 x +1
< 2 x2 when x ≥ 1000, we can choose k = 1000 and c =
3
2
. (Note that there are an infinite number of correct choices of
k and c that will work. This is just one approach to finding
them.)
8.
a)
Given:
f(x)=2x2 + x3logx
When x>0, we have the property logx ≤ x
f(x)= 2x2 + x3logx ≤ 2x2 + x4
The largest power of x mentioned in the definition of f(x) is the
smallest n for which f(x) is O(xn)
n=4
When x > 2, we have the property x2 > x > 2
For convience sake, we will choose k=2 and thus use x > 2
|f(x)|=|2x2+x3logx| ≤ |2x2 + x4| ≤ |2x2| + |x4|
=2x2 + x4 ≤ x4 + x4
=2x4
=2|x4|
Thus we need to choose C to be at least 2. Let us then take C = 2

b) Given:
f(x)=3x5 + (logx)4
The largest power of x mentioned in the definition of f(x) is the
smallest n for which f(x) is O(xn)
n=5
When x.0, we have the property logx ≤ x
When x>1, we also have the property x4 < x5
For convience sake, we will choose k=1 and thus us x>1
|f(x)|=|3x5 + (logx)4| ≤ |3x5| + |(log⁡x)4|
=3x5 + (logx)4 ≤ 3x5 + x4
=4x5
=4|x5|
Thus we nned to choose C to be at least 4. Let us then take C = 4
By the definition of the Big-O notation f(x) =
O(x5) with k=1 and C = 2

c) Given:
f(x)=(x4 + x2 + 1) / (x4 + 1)
Let us determine the quotient (with remainder) of the given
using long division.
The quotient is then 1 with remainder x2 and thus we can
rewrite the given fractions as:
f(x)=(x4 + x2 + 1) / (x4 + 1)=(1 + x2) / ( x4 + 1)
The largest power of x of the quotient is the smallest n for
which f(x) is O(xn).
n=0
For convenience sake, we will choose k=0 and thus x>0
|f(x)|=|1 + (x2)/(x4 + 1)| ≤ | 1 |+|(x2) / (x4 + 1)|
=1+(x2)/(x4 + 1) ≤ 1+1
<2
=2⋅| 1 |
=2.| x0 |
Thus we need to choose C to be at least 2. Let us then take C = 2

By the definition of the Big-O notation. f(x) = O(x0) =


O(1) with k = 0 and C = 2
d) x > 0 we have |f(0)| ≤ (x4 + 5x) / (x4 + 1)
When x > 3 then x4 + 5x < x4 + 1, so we have for k = 3, n = 0
|f(x)| ≤ 1 = x0
So that C = 1
12.
f(x) = O(g(x)) if there exists a positive real number M and a real
number x0 such that
|f(x)| ≤ Cg(x)∣f(x)∣ ≤ Cg(x) for all x ≥ x0

|x2 | > xlogx for positive integers.


So. x2 is not O ( x*log(x) ).
Page 200
10.

Page 208
6.
Suppose that a∣b and c∣d. It follows that we have b = k1a
and d = k2c for integers k1,k2. It follows that
bd = (k1a)(k2c) = (k1k2)(ac)
Let k be equal to the integer k1k2. We see that (bd) = k(ac).
Thus, bd is divisible by ac.
Page 209
12
Assume that a ≡ b ( mod m). This means that m | a - b, say a – b
= mc, so that a = b + mc. Now let us compute a mod m. We
know that b = qm + r for some nonnegativerless than m( namely,
r = b mod m). Therefore we can write a = qm + r + mc = (q +
c)m +r. By definition this means thatrmust also equal a mod m.
That is what we wanted to prove.

20.
From a≡b (mod m) we know that b = a + sm for some integers.
Similarly,d = c + tm.Subtracting, we haveb d= (a - c) + (s - t)m,
which means that a – c≡b - d(mod m)
31.
a) GR QRW SDWJR
b) QB ABG CNFF TB
c) QX UXM AHJJ ZX
32.
a)
b)
C)
Page 218
20. GCD
a) GCD = 1
b) GCD = 1
c) GCD = 17
d) GCD = 1
e) GCD = 5
f) GCD = 1

26.
- GCD = 23345
- LCM = 273852711

You might also like