CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

Embed Size (px)

Citation preview

  • 8/12/2019 CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    1/6

    CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    SOLUTIONS

    1. (Logic)

    (1) Show that ( p q ) and ( p) q are logically equivalent.(2) Determine the truth value of each of these statements if the universe

    of discourse of each variable consists of all integers.(a) n (n 2 0) (b) n (n 2 = 2) (c) n (n 2 n ) (d) n (n 2 < 0)

    (3) Determine the truth value of each of these statements if the universeof discourse of each variable consists of all real numbers.(a) x(x

    2 = 2) (b) x(x2 = 1) (c)x(x2 +2 1) (d)x(x2 = x)

    Solution:

    (1) Using a truth table:

    p q p p q ( p q ) ( p) q T T F T F FT F F F T TF T T F T TF F T T F F

    They have the same truth values

    (2) (a) TRUE ( n 2 is always in fact non-negative).

    (b) FALSE ( 2 is not an integer.)(c) TRUE (it can be false only for 0 < n < 1, but there are no integers

    between 0 and 1.)

    (d) FALSE (it is the negation of (a)).

    (3) (a) TRUE ( 2 is a real number.)(b) FALSE ( i = 1 is not real.)(c) TRUE (since x2 0).(d) FALSE (counterexamples: x = 0 and x = 1.)

    1

  • 8/12/2019 CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    2/6

    2. (Sets)

    1. Let Ai = {1, 2, 3, . . . , i } for i = 1 , 2, 3, . . . . Find(a)

    n

    i=1

    A i (b)n

    i=1

    A i

    2. Let Ai = {. . . , 2, 1, 0, 1, 2, 3, . . . , i } for i = 1 , 2, 3, . . . . Find(a)

    n

    i=1

    A i (b)n

    i=1

    A i

    3. Let Ai be the set of all nonempty bit strings (that is, bit strings over

    {0, 1} of length at least one) of length not exceeding i. Find(a)

    n

    i=1A i (b)

    n

    i=1A i

    Solution:

    1. (a)n

    i=1

    A i = A n = {1, 2, 3, . . . , n }

    (b)

    n

    i=1A i = A 1 = {1}

    2. (a)n

    i=1

    A i = A n = {. . . , 2, 1, 0, 1, 2, 3, . . . , n }

    (b)n

    i=1

    A i = A 1 = {. . . , 2, 1, 0, 1}

    3. (a)n

    i=1

    A i = A n = {s {0, 1} | 1 |s| n}

    (b)n

    i=1

    A i = A 1 = {0, 1}

    2

  • 8/12/2019 CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    3/6

    3. (Relations) For each of the following relations dened on the set of positiveintegers Z + , determine which ones are partial orders, and which ones areequivalence relations (some may be both or none).

    (a) x Ry x y.(b) x Ry x < y .(c) x Ry x < y .(d) x Ry for every prime number p, if p divides x then p divides y.(e) x Ry for every prime number p, p divides x if and only if p divides y.(f) x Ry x = y.

    Solution:

    (a) Partial order.

    (b) None (it is not reexive).

    (c) Partial order (it is the same as x y.)(d) None. It is not an order relation because it is not antisymmetric (e.g.,

    2R4 and 4R2, but 2 = 4.) It is not an equivalence relation becauseit is not symmetric (e.g. 2 R6, but 6 is not related to 2.)(e) Equivalence x and y are related precisely when they are divided by

    the same set of primes. It is not a partial order because it is notantisymmetric (e.g., 12 R18 and 18R12, because both are dividedpreciselly by the primes 2 and 3, but 12 = 18.)

    (f) Both, it is an equivalence relation (obviously), and it is also a partial order relation. Note that it veries the antisymmetric property, be-cause x

    Ry and y

    Rx means x = y and y = x, which in fact implies

    x = y.

    3

  • 8/12/2019 CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    4/6

    4. (Algorithms) Find a notation for the value f (n ) returned by the followingrecursive algorithm:

    1: procedure f(n)2: if n 1 then3: return 14: else5: return (2 * f(n-2))6: end f

    Solution:

    (Note that the question is about the value of the function, no about thecomplexity of the algorithm.)

    We have that if n 0 then f (n ) = 1, otherwisef (n ) = 2 f (n 2) = 4 f (n 4) = 8 f (n 6) = 16 f (n 8) = 32 f (n 10) = If n is even the last term is 2 n/ 2f (0) = 2 n/ 2 .

    If n is odd then we get 2(n1)/ 2f (1) = 2 (n1) / 2 = 1 2 2n/ 2 .

    Hence 1 2 2n/ 2 f (n ) 2n/ 2, so the answer is

    f (n ) = (2n/ 2

    )

    4

  • 8/12/2019 CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    5/6

    5. (Integers)

    (a) Remember that (m ) = number of positive integers not greater than

    m and relatively prime to m . Find (11).(b) Find 3 98765432 mod 11 = the remainder when 3 98765432 is divided by 11.

    In other words, if 398765432 = 11 q + r where q and r are integers, and0 r < 11, what is r?

    Solution:

    (a) Since 11 is prime we have (11) = 11

    1 = 10 .

    (b) We have 98765432 2 (mod 10), hence (by Eulers theorem) 3 98765432 32 9 (mod 11). Hence 398765432 mod 11 = 9 .

    5

  • 8/12/2019 CS 310 (sec 20) - Spring 2005 - Midterm Exam (solutions)

    6/6

    6. (Counting) How many ways are there to select four pieces of fruit from abowl containing apples, oranges, and pears if the order in which the piecesare selected does not matter, only the type of fruit and not the individualpiece matters, and there are at least four pieces of each type of fruit in thebowl?

    Solution:

    This is equivalent to nding the number of non-negative solutions to thefollowing equation:

    x1 + x2 + x3 = 4 .

    The answer is C (4 + 3 1, 4) = C (6, 4) = 6!4!2! = 15 .

    6