WGU C960 DISCRETE MATHEMATICS II
COMPREHENSIVE EXAM STUDY GUIDE
WITH COMPLETE SOLUTIONS
●● Pre-Assessment: Algorithms - Algorithm Structures:
Question 2:
Given this pseudocode:
S = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
x=2
While(x<11):
For i in S:
If 0 ≡ i mod x and i ≠ x:
delete i from S
end-If
end-For
x=x+1
end-While
What is S at the end of this code?
Answer: {2, 3, 5, 7, 11, 13, 17, 19}
,This code is nothing but finding the prime numbers from the given set.
Start from x = 2
1) When x = 2, we have to delete that elements from S which satisfy 0
== i mod 2 and i ≠ 2 i.e multiples of 2 except 2. So we remove {4, 6, 8,
10, 12, 14, 16, 18, 20}
x = x+1
2) When x = 3, we have to delete that elements from S which satisfy 0
== i mod 3 and i ≠ 3 i.e multiples of 3 except 3. So we remove {6, 9, 12,
15, 18}
x = x+1
3) When x = 4, we have to delete that elements from S which satisfy 0
== i mod 4 and i ≠ 4 i.e multiples of 4 except 4. So we remove {8, 12,
16, 20}
x = x+1
4) When x = 5, we have to delete that elements from S which satisfy 0
== i mod 5 and i ≠ 5 i.e multiples of 5 except 5. So we remove {10, 15,
20}
x = x+1
5) When x = 6, we have to delete that elements from S which satisfy 0
== i mod 6 and i ≠ 6 i.e multiples of 6 except 6. So we remove {12, 18}
x = x+1
6) When x = 7, we have to delete that elements from S which satisfy 0
== i mod 7 and i ≠ 7 i.e multiples of 7 except 7. So we remove {14}
x = x+1
,7) When x = 8, we have to delete that elements from S which satisfy 0
== i mod 8 and i ≠ 8 i.e multiples of 8 except 8. So we remove {16}
x = x+1
8) When x = 9, we have to delete that elements from S which satisfy 0
== i mod 9 and i ≠ 9 i.e multiples of 9 except 9. So we remove {18}
x = x+1
9) When x = 10, we have to delete that elements from S which satisfy 0
== i mod 10 and i ≠ 10 i.e multiples of 10 except 10. So we remove
{20}
x = x+1
Now x = 11, we break the while loop and the program is terminated.
So, after deleting the above elements, we are left with
{2, 3, 5, 7, 11, 13, 17, 19}
●● Pre-Assessment: Algorithms - Algorithm Structures:
Question 3:
Given the pseudocode fragment:
x := 2
count := 4
while (count > 0)
x := 2 * x
count := count - 1
, End-while
What is the final value for x?
Answer: 32
x=2*2
x=2*4
x=2*8
x = 2 * 16
x = 32
●● Pre-Assessment: Algorithms - Analyzing Algorithms:
Question 4:
Function Sampler (Sequence Data)
Set Sample to an empty sequence
Set N to the length of Data
While N>=1
Append element N of Data to Sample
N:= N/2
Return Sample
What is the worst-case run time for Function Sampler?
Answer: O(log₂N)
COMPREHENSIVE EXAM STUDY GUIDE
WITH COMPLETE SOLUTIONS
●● Pre-Assessment: Algorithms - Algorithm Structures:
Question 2:
Given this pseudocode:
S = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
x=2
While(x<11):
For i in S:
If 0 ≡ i mod x and i ≠ x:
delete i from S
end-If
end-For
x=x+1
end-While
What is S at the end of this code?
Answer: {2, 3, 5, 7, 11, 13, 17, 19}
,This code is nothing but finding the prime numbers from the given set.
Start from x = 2
1) When x = 2, we have to delete that elements from S which satisfy 0
== i mod 2 and i ≠ 2 i.e multiples of 2 except 2. So we remove {4, 6, 8,
10, 12, 14, 16, 18, 20}
x = x+1
2) When x = 3, we have to delete that elements from S which satisfy 0
== i mod 3 and i ≠ 3 i.e multiples of 3 except 3. So we remove {6, 9, 12,
15, 18}
x = x+1
3) When x = 4, we have to delete that elements from S which satisfy 0
== i mod 4 and i ≠ 4 i.e multiples of 4 except 4. So we remove {8, 12,
16, 20}
x = x+1
4) When x = 5, we have to delete that elements from S which satisfy 0
== i mod 5 and i ≠ 5 i.e multiples of 5 except 5. So we remove {10, 15,
20}
x = x+1
5) When x = 6, we have to delete that elements from S which satisfy 0
== i mod 6 and i ≠ 6 i.e multiples of 6 except 6. So we remove {12, 18}
x = x+1
6) When x = 7, we have to delete that elements from S which satisfy 0
== i mod 7 and i ≠ 7 i.e multiples of 7 except 7. So we remove {14}
x = x+1
,7) When x = 8, we have to delete that elements from S which satisfy 0
== i mod 8 and i ≠ 8 i.e multiples of 8 except 8. So we remove {16}
x = x+1
8) When x = 9, we have to delete that elements from S which satisfy 0
== i mod 9 and i ≠ 9 i.e multiples of 9 except 9. So we remove {18}
x = x+1
9) When x = 10, we have to delete that elements from S which satisfy 0
== i mod 10 and i ≠ 10 i.e multiples of 10 except 10. So we remove
{20}
x = x+1
Now x = 11, we break the while loop and the program is terminated.
So, after deleting the above elements, we are left with
{2, 3, 5, 7, 11, 13, 17, 19}
●● Pre-Assessment: Algorithms - Algorithm Structures:
Question 3:
Given the pseudocode fragment:
x := 2
count := 4
while (count > 0)
x := 2 * x
count := count - 1
, End-while
What is the final value for x?
Answer: 32
x=2*2
x=2*4
x=2*8
x = 2 * 16
x = 32
●● Pre-Assessment: Algorithms - Analyzing Algorithms:
Question 4:
Function Sampler (Sequence Data)
Set Sample to an empty sequence
Set N to the length of Data
While N>=1
Append element N of Data to Sample
N:= N/2
Return Sample
What is the worst-case run time for Function Sampler?
Answer: O(log₂N)