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

Chapter 5-7: Counting

5.1 The Basics of Counting


1. How many different bit strings of length seven are there?
2^7 = 128
There are 128 different bit strings of length seven.
2. How many different license plates can be made if each plate contains
a sequence of three uppercase English letters followed by three digits
(and no sequences of letters are prohibited, even if they are obscene)?
For the first position (letter), there are 26 possible uppercase English
letters (A to Z).

For the second position (letter), there are also 26 possibilities.

For the third position (letter), again, there are 26 possibilities.

For the fourth position (digit), there are 10 possibilities (0 to 9).

For the fifth position (digit), there are 10 possibilities.

For the sixth position (digit), there are 10 possibilities.

26 * 26 * 26 * 10 * 10 * 10 = 17,576,000
There are 17,576,000 different license plates that can be made using this
format
3. Counting Functions: How many functions are there from a set with
m elements to a set with n elements?
There are n^m functions from a set with m elements to a set with n
elements. This is because for each of the m elements in the domain,
there are n choices for its image in the codomain. Since the choices for
each element are independent, we can multiply the number of choices
for each element to get the total number of functions.

4. Counting One-to-One Functions: How many one-to-one functions


are there from a set with m elements to one with n elements?
A one-to-one function, also known as an injective function, is a function
where each element in the codomain is mapped to by at most one
element in the domain. The number of injective functions from a set
with m elements to a set with n elements is given by the formula n(n-1)
(n-2)...(n-m+1), which can also be written as n!/(n-m)! if m <= n. If m >
n, then there are no injective functions from a set with m elements to a
set with n elements.
5. Counting bijection Functions: How many one-to-one functions are
there from a set with m elements to one with n elements?
A bijection is a function that is both one-to-one (injective) and onto
(surjective). This means that each element in the codomain is mapped to
by exactly one element in the domain. For a function from a set with m
elements to a set with n elements to be bijective, it is necessary that m =
n. If m = n, then the number of bijective functions is n!, which is the
number of ways to arrange n distinct objects. If m != n, then there are no
bijective functions from a set with m elements to a set with n elements.
6. Each user on a computer system has a password, which is six to eight
characters long, where each character is an uppercase letter or a digit.
Each password must contain at least one digit. How many possible
passwords are there?
There are 26 uppercase letters and 10 digits, so there are 36 possible
characters.
Beacause the passwords is six to eight characters long so the total
number of passwords without the restriction is:
36^6 + 36^7 + 36^8
The number of passwords that do not contain any digits will be:
26^6 + 26^7 + 26^8
Therefore, the total number of possible passwords that meet the given
criteria is (36^6 + 36^7 + 36^8) - (26^6 + 26^7 + 26^8) =
2,634,195,994,144.
7. How many bit strings of length eight either start with a 1 bit or end
with the two bits 00?
There are 2^7 bit strings of length eight that start with a 1 bit.
There are 2^6 bit strings of length eight that end with the two bits 00.
There are 2^5 strings that both start with a 1 bit and end with the two
bits 00.
The total number of bit strings of length eight that either start with a 1
bit or end with the two bits 00 is 2^7 + 2^6 - 2^5 = 192.
8. How many bit strings of length four do not have two consecutive 1s?
There are 2^4 = 16 total bit strings of length four.
There are three positions where two consecutive 1s can occur in a bit
string of length four: at the first and second positions so there are 2^2 =
4 possible bit strings for the remaining two bits.
There are a total of 3 * 4 = 12 bit strings of length four that have two
consecutive 1s.
The number of bit strings of length four that do not have two
consecutive 1s is 16 - 12 = 4.
9. How many bit strings of length ten both begin and end with a 1?
There are 2^8 = 256 bit strings of length ten that both begin and end
with a 1.
10. How many positive integers between 5 and 31
a) are divisible by 3?
The smallest multiple of 3 between 5 and 31 is 6 and the largest multiple
of 3 between 5 and 31 is 30. There are (30 - 6)/3 + 1 = 9 positive
integers
b) are divisible by 4?
The smallest multiple of 4 between 5 and 31 is 8 and the largest multiple
of 4 between 5 and 31 is 28. There are (28 - 8)/4 + 1 = 6 positive
integers
c) are divisible by 3 and by 4?
The only positive integer between 5 and 31 that is divisible by both 3
and 4 is the least common multiple of 3 and 4, which is 12. So there is
only 1 positive integer
11. How many positive integers between 50 and 100
a) are divisible by 7?
The smallest multiple of 7 between 50 and 100 is 56 and the largest
multiple of 7 between 50 and 100 is 98. There are (98 - 56)/7 + 1
= 7 positive integers 
b) are divisible by 11?
The smallest multiple of 11 between 50 and 100 is 55 and the largest
multiple of 11 between 50 and 100 is 99. There are (99 - 55)/11 + 1
= 5 positive integers 
c) are divisible by both 7 and 11?
The least common multiple of 7 and 11 is 77. Since there is only one
multiple of 77 between 50 and 100, there is only 1 positive integer
12. How many positive integers less than 1000
a) are divisible by 7?
The smallest multiple of 7 less than 1000 is 7 and the largest multiple of
7 less than 1000 is 994. There are (994 - 7)/7 + 1 = 142 positive integers
b) are divisible by 7 but not by 11?
The least common multiple of 7 and 11 is 77.
The smallest multiple of 77 less than 1000 is 77 and the largest multiple
of 77 less than 1000 is 924. There are (924 - 77)/77 +1 = 12 positive
integers
Therefore, there are 142 -12 = 130 positive integers less than1000that
are divisible by 7 but not by 11.
c) are divisible by both 7 and 11?
There are (924 - 77)/77 +1 = 12 positive integers less than 1000 that are
divisible by both 7 and 11. 
d) are divisible by either 7 or 11?
The smallest multiple of11less than1000is11and the largest multiple
of11less than 1000 is 990. There are (990 -11)/11 +1 = 90 positive
integers
There are 142 +90 -12 = 220 positive integers less than1000that are
divisible by either 7 or 11.
e) are divisible by exactly one of 7 and 11?
There are 130 positive integers less than 1000 that are divisible by 7 but
not by 11
There are 90 - 12 = 78 positive integers less than 1000 that are divisible
by 11 but not by 7
There are 130 + 78 = 208 positive integers less than 1000 that are
divisible by exactly one of 7 and 11.
f ) are divisible by neither 7 nor 11?
There are a total of 999 positive integers less than 1000. 
there are 220 positive integers less than 1000 that are divisible by either
7 or 11.
there are999 -220 = 779 positive integers less than 1000that are divisible
by neither 7 nor 11.
g) have distinct digits?
There is only 9 number with one digit (except the number 0) and it has
distinct digits
For numbers with two digits, there are 9 choices for the first digit (it
cannot be 0) and 9choices for the second digit (it cannot be the same as
the first digit), so there are 9 * 9 = 81 numbers with two digits that have
distinct digits. 
For numbers with three digits, there are 9 choices for the first digit (it
cannot be 0), 9 choices for the second digit (it cannot be the same as the
first digit), and 8 choices for the third digit (it cannot be the same as
either the first or second digits), so there are 9 *9 *8 = 648 
there is a total of 9 +81 +648 = 738 positive integers
h) have distinct digits and are even?
For numbers with two digits, there are4choices for the second digit (it
must be an even digit:2, 4, 6 or 8) and8choices for the first digit (it
cannot be 0 or the same as the second digit), so there are 4 *8 = 32 even
numbers with two digits that have distinct digits.
For numbers with three digits, there are 4 choices for the third digit (it
must be an even digit:2, 4, 6 or 8),8choices for the first digit (it cannot
be 0 or the same as the third digit), and 8 choices for the second digit (it
cannot be the same as either the first or third digits), so there are 4 *8 *8
= 256 even numbers with three digits that have distinct digits. 
There is a total of 32 +256 = 288 even positive integers
13. How many positive integers between 100 and 999 inclusive
a) are divisible by 7?
The smallest multiple of 7 between 100 and 999 is 105 and the largest
multiple of 7 between 100 and 999 is 994. There are (994 - 105)/7 + 1
= 128 positive integers
b) are odd?
The smallest multiple of 7 between 100 and 999 is 105 and the largest
multiple of 7 between 100 and 999 is 994. There are (994 - 105)/7 + 1
= 128 positive integers
c) have the same three decimal digits?
There are 9 positive integers between 100 and 999 that have the same
three decimal digits: 111, 222, …, 999.
d) are not divisible by 4?
The smallest multiple of 4 between 100 and 999 is 100 and the largest
multiple of 4 between 100 and 999 is 996. There are (996 -100)/4 +1
= 225 positive integers
There are 900 -225 = 675 positive integers between 100 and 999 that are
not divisible by 4.
e) are divisible by 3 or 4?
The smallest multiple of 3 between 100 and 999 is 102 and the largest
multiple of 3 between 100 and 999 is 999. There are (999 -102)/3 +1
= 300 positive integers
there are 225 positive integers between 100 and 999 that are divisible
by4.
The smallest multiple of 12 between 100 and 999 is 108 and the largest
multiple of 12 between 100 and 999 is 996. There are (996 -108)/12 +1
= 75 positive integers
There are 300 +225 -75 = 450 positive integers
f ) are not divisible by either 3 or 4?
There are 450 positive integers between 100 and 999 that are divisible
by either 3 or 4.
There are 900 -450 = 450 positive integers between 100 and 999 that are
not divisible by either 3 nor 4.
g) are divisible by 3 but not by 4?
There are 300 positive integers between 100 and 999 that are divisible
by 3 and 75 positive integers between 100 and 999that are divisible by
both 3and 4.
There are300 -75 = 225 positive integers between100and999that are
divisible by 3 but not by 4.
h) are divisible by 3 and 4?
There are 75 positive integers between 100 and 999 that are divisible by
both 3 and 4.
14. How many one-to-one functions are there from a set with five
elements to sets with the following number of elements?

a) 4
The number of one-to-one functions from a set with 5 elements to a set
with 4 elements is 4!/(4-5)! = 4!/(-1)! = 0, since there are no one-to-one
functions from a larger set to a smaller set.
b) 5
The number of one-to-one functions from a set with 5 elements to a set
with 5 elements is 5!/(5-5)! = 5!/0! = 120.
c) 6
The number of one-to-one functions from a set with 5 elements to a set
with 5 elements is 5!/(5-5)! = 5!/0! = 120.
d) 7
The number of one-to-one functions from a set with 5 elements to a set
with 7 elements is 7!/(7-5)! = 7!/2! = 2520.
15. How many bit strings of length seven either begin with two 0s or end
with three 1s?
There are 2^5 bit strings of length seven that begin with two 0s
There are 2^4 bit strings of length seven that end with three 1s.
There are 2^2 bit strings that both begin with two 0s and end with three
1s.
The total number of bit strings of length seven that either begin with two
0s or end with three 1s is 2^5 + 2^4 - 2^2 = 44.
16. How many bit strings of length 10 either begin with three 0s or end
with two 0s?
There are 2^7 bit strings of length 10 that begin with three 0s.
There are 2^8 bit strings of length 10 that end with two 0s.
There are 2^5 such bit strings that both begin with three 0s and end with
two 0s.
The total number of bit strings of length 10 that either begin with three
0s or end with two 0s is 2^7 + 2^8 - 2^5 = 320.
17. How many bit strings of length 8 begin with 11 or end with 00?
There are 2^6 bit strings of length 8 that begin with 11.
There are 2^6 bit strings of length 8 that end with 00.
There are 2^4 such bit strings  that both begin with 11 and end with 00.
The total number of bit strings of length 8 that either begin with 11 or
end with 00 is 2^6 + 2^6 - 2^4 = 112.
18. Let B be the set {a, b, c}. How many functions are there from B2 to
B?
Since B has 3 elements, B^2 has 3^2 = 9 elements. A function from B^2
to B assigns an element of B to each element of B^2. Since there are 3
choices for the value of the function at each element of B^2, there are
3^9 = 19683 functions from B^2 to B.
19. How may bit strings with length 10 that has exactly three 1s and end
with 0?
The number of ways to do this is given by the binomial coefficient “9
choose 3”, which is equal to 9!/(3!6!) = 84. Therefore, there are 84 bit
strings of length 10 that have exactly three 1s and end with 0.
20. A game consisting of flipping a coin ends when the player gets two
heads in a row, two tails in a row or flips the coin four times. In how
many ways can the game end? 
1. The player gets three heads in a row: There are two possibilities for
this to happen: HH, THH,HTHH.
2. The player gets three tails in a row: There are two possibilities for
this to happen: TT, TTH, THTT.
3. The player flips the coin four times without getting two heads or
two tails in a row: There are two possibilities for this to happen:
HTHT, THTH.
Therefore, there are a total of 3 + 3 + 2 = 8 ways for the game to end.
7.1 Recurrence Relations
1. a) Find a recurrence relation for the number of permutations of a set
with n elements.
A permutation of a set with n elements can be obtained by choosing one
of the n elements to be the first element and then permuting the
remaining n-1 elements. Therefore,  we have the recurrence relation P(n)
= n * P(n-1) for n >= 1

b) Use this recurrence relation to find the number of permutations of a


set with n elements using iteration.
P(1) = 1 * P(0) = 1
P(2) = 2 * P(1) = 2
P(3) = 3 * P(2) = 6

P(n) = n * P(n-1)
Therefore, the number of permutations of a set with n elements is n!.

2. a) Find a recurrence relation for the number of bit strings of length n


that do not contain three consecutive 0s.
b) What are the initial conditions?
c) How many bit strings of length seven do not contain
three consecutive 0s?
3. What is the solution of the recurrence relation an = an-1 + 2an-2 with
a0 = 2 and a1 = 7?
The characteristic equation of this recurrence relation is r^2 - r - 2 = 0.
Solving this equation, we find that the roots are r1 = 2 and r2 = -1.
Therefore, the general solution of the recurrence relation is given by an
= c1 * 2^n + c2 * (-1)^n for some constants c1 and c2.
To find the values of c1 and c2, we use the initial conditions a0 = 2 and
a1 = 7. Substituting n = 0 into the general solution, we get a0 = c1 * 2^0
+ c2 * (-1)^0 = c1 + c2. Since a0 = 2, we have c1 + c2 = 2. Substituting
n = 1 into the general solution, we get a1 = c1 * 2^1 + c2 * (-1)^1 = 2c1
- c2. Since a1 = 7, we have 2c1 - c2 = 7. Solving this system of two
equations for c1 and c2, we find that c1 = 3 and c2 = -1.
Therefore, the solution of the recurrence relation an = an-1 + 2an-2 with
a0 = 2 and a1 = 7 is given by an = 3 * 2^n - (-1)^n.
7.3 Divide-and-Conquer Algorithms and recurrence
Relations
1. How many comparisons are needed for a binary search in a set of 64
elements?
In a binary search, the number of comparisons needed to search for an
element in a set of n elements is at most the smallest integer k such that
2^k >= n. In other words, the number of comparisons needed is at most
the ceiling of log2(n).
For a set of 64 elements, the number of comparisons needed for a binary
search is at most the ceiling of log2(64) = 6. Therefore, at
most 6 comparisons are needed for a binary search in a set of 64
elements.

2. Suppose that f(n) = f(n/3) + 1 when n is a positive integer divisible by


3, and f(1) = 1. Find
a) f(3)
Since 3 is divisible by 3, f(3) = f(3/3) + 1 = f(1) + 1 = 1 + 1 = 2.
b) f(27)
Since 27 is divisible by 3, we have f(27) = f(27/3) + 1 = f(9) + 1. Since 9
is divisible by 3, we have f(9) = f(9/3) + 1 = f(3) + 1. Substituting the
value of f(3) that we found in part (a), we get f(9) = 2 + 1 = 3. Therefore,
f(27) = f(9) + 1 = 3 + 1 = 4.
c) f(729)
Since 729 is divisible by 3, we have f(729) = f(729/3) + 1 = f(243) + 1.
Since 243 is divisible by 3, we have f(243) = f(243/3) + 1 = f(81) + 1.
Since 81 is divisible by 3, we have f(81) = f(81/3) + 1 = f(27) + 1.
Substituting the value of f(27) that we found in part (b), we get f(81) = 4
+ 1 =5. Therefore, f(243)=f(81)+1=5+1=6 andf(729)=f(243)+1=6+1=7.
3. Suppose that f(n) = 2f(n/2) + 3 when n is an even positive integer, and
f(1) = 5. Find
a) f(2)
Since 2 is an even positive integer, we have f(2) = 2f(2/2) + 3 = 2f(1) + 3
= 2 * 5 + 3 = 13.
b) f(8)
Since 8 is an even positive integer, we have f(8) = 2f(8/2) + 3 = 2f(4) +
3. Since 4 is an even positive integer, we have f(4) = 2f(4/2) + 3 = 2f(2)
+ 3. Substituting the value of f(2) that we found in part (a), we get f(4) =
2 * 13 + 3 =29. Therefore, f(8) = 2f(4) + 3 = 2 *29+3= 61.
c) f(64)
Since64is an even positive integer, we have f(64)=2f(64/2)+3=2f(32)+3.
Since32is an even positive integer, we have f(32)=2f(32/2)+3=2f(16)+3.
Since16is an even positive integer, we have f(16)=2f(16/2)+3=2f(8)+3.
Substituting the value of f(8) that we found in part (b), we get
f(16)=261+3=125. Therefore, f(32)=2f(16)+3=2125+3=253 and
f(64)=2f(32)+3=2*253+3=509.
d) f(1024).
Since1024is an even positive integer, we have
f(1024)=2f(1024/2)+3=2f(512)+3. Since512is an even positive integer,
we have f(512)=2f(512/2)+3=2f(256)+3. Since256is an even positive
integer, we have f(256)=2f(256/2)+3=2f(128)+3. Since128is an even
positive integer, we have f(128)=2f(128/2)+3=2f(64)+3. Substituting the
value of f(64) that we found in part ©, we get f(128)=2*509+3=1021.
Therefore, f(256)=2f(128)+3=2045 and f(512)=4091 and f (1024)=8187.
4. Suppose that f(n) = f(n/5) + 3n when n is a positive integer divisible
by 5, and f(1) = 4.
Find
a) f(5)
Since 5 is divisible by 5, we have f(5) = f(5/5) + 3 * 5 = f(1) + 15 = 4 +
15 = 19.
b) f(125)
Since 125 is divisible by 5, we have f(125) = f(125/5) + 3 * 125 = f(25)
+ 375. Since 25 is divisible by 5, we have f(25) = f(25/5) + 3 * 25 = f(5)
+ 75. Substituting the value of f(5) that we found in part (a), we get f(25)
= 19 + 75 =94. Therefore, f(125) = f(25) +375=94+375= 469.
c) f(3125)
Since 3125 is divisible by 5, we have
f(3125)=f(3125/5)+33125=f(625)+9375. Since 625 is divisible by 5, we
have f(625)=f(625/5)+3625=f(125)+1875. Substituting the value of
f(125) that we found in part (b), we get f(625)=469 + 1875=2344.
Therefore, f(3125)=f(625)+ 9375= 2344 + 9375= 11719.

You might also like