Professional Documents
Culture Documents
2017 Semester 3
Introduction
The Teacher
Course Learning Outcomes
Topics
Assessment
Logic
Logic Statements
Short Circuit Evaluation
Weekly Activity (October 20, Early Feedback)
Logic in Programming
Weekly Activity (October 27, Early Feedback)
Reference Book:
Kenneth Rosen, “Discrete Mathematics and Its Applications”, 7th Edition
Assessment:
T T
T F
F T
F F
T T
T F
F T
F F
T T T
T T F
T F T
T F F
F T T
F T F
F F T
F F F
Logic in Programming
private static String sayNumber(int before, int number) { // say a 3-digit number
int hundreds = number / 100;
number = number - hundreds * 100;
int tens = number / 10;
number = number - tens * 10;
int ones = number;
String viet = "";
if (hundreds == 0 && before > 0) {
viet = viet + " khong tram";
if (before > 0 && tens == 0 && ones != 0) {
viet = viet + " le";
}
}
if (hundreds > 0) {
viet = viet + " " + sayDigit(hundreds) + " tram";
}
if (hundreds != 0 && tens == 0 && ones != 0) {
viet = viet + " le";
}
if (tens == 1) {
viet = viet + " muoi";
}
if (tens > 1) {
viet = viet + " " + sayDigit(tens) + " muoi";
}
if (ones == 5 && tens > 0) {
viet = viet + " lam";
}
if (ones == 5 && tens == 0) {
viet = viet + " nam";
}
if (ones > 0 && ones != 5) {
viet = viet + " " + sayDigit(ones);
}
return viet;
}
P = “hundreds is zero”
Q = “tens is zero”
R = “ones is zero”
Say “le” if (~P and Q and ~R) is True
if (hundreds != 0 && tens == 0 && ones != 0) {
viet = viet + " le";
}
0 T T T F
5 T T F F **
30 T F T F
46 T F F F
200 F T T F
908 F T F T
650 F F T F
312 F F F F
** we still say “le” if the value before this 3-digit group is greater than zero
Example: 4,005
if (hundreds == 0 && before > 0) {
viet = viet + " khong tram";
if (before > 0 && tens == 0 && ones != 0) {
viet = viet + " le";
}
}
MATH 2081 October 27, 2017 Weekly Activity (Early Feedback)
1. Convert the decimal number 186 to 8-bit binary. 186 = ____________________ (binary)
3. Write the truth tables for NOT, AND, OR, XOR and draw their logic gates.
A not A
A B A and B A or B A xor B
0 0
0 1
1 0
1 1
4. Given the circuit of the Full Adder, complete the truth table
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
Truth Table
4-bit input (Digit→ Binary) 7-bit output (which segments should light up?)
Digit A B C D a b c d e f g
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0 0 1 1 0 0 1 1
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1
This is the final circuit that implements the 7-segment LED. The truth table, which maps the
inputs to the outputs, is translated into wires, NOT gates, AND gates and OR gates.
6. Using a truth table, prove that ~(P and Q and R) is equal to ~P or ~Q or ~R
P Q R ~(P and Q and R) ~P or ~Q or ~R
T T T
T T F
T F T
T F F
F T T
F T F
F F T
F F F
if (x-r >= x1 and x+r <= x2 and y-r >= y1 and y+r <= y2) {
Then K is inside G
}
8. In computer games, how do you check if two hitboxes G1 and G2 have collided
(overlapped each other)? A hitbox is a bounding rectangle containing a game object.
Assume that the hitboxes are not rotated. The corners of G1 are (x1,y1) and (x2,y2)
while the corners of G2 are (x3,y3) and (x4,y4).
Transmitted
Sent ciphertext Received
message message
Encrypt Decrypt
32^1 mod 91 = 32
32^2 mod 91 = 1024 mod 91 = 23
32^4 mod 91 = (32^2)(32^2) mod 91
= (32^2 mod 91)(32^2 mod 91) mod 91
= 23 x 23 mod 91 = 529 mod 91 = 74
32^8 mod 91 = (32^4)(32^4) mod 91
= (32^4 mod 91)(32^4 mod 91) mod 91
= (74 x 74) mod 91 = 5476 mod 91 = 16
32^16 mod 91 = (32^8)(32^8) mod 91
= (32^8 mod 91)(32^8 mod 91) mod 91
= (16 x 16) mod 91 = 256 mod 91 = 74
Second letter “E” = 5
Transmitted
Sent ciphertext Received
message message
Encrypt Decrypt
where
31^1 mod 91 = 31 mod 91 = 31
31^2 mod 91 = 961 mod 91 = 51
31^4 mod 91 = (31^2)(31^2) mod 91
= (31^2 mod 91)(31^2 mod 91) mod 91
= (51)(51) mod 91 = 2601 mod 91 = 53
31^8 mod 91 = (31^4)(31^4) mod 91
= (31^4 mod 91)(31^4 mod 91) mod 91
= (53)(53) mod 91 = 79
31^16 mod 91 = (31^8)(31^8) mod 91
= (31^8 mod 91)(31^8 mod 91) mod 91
= (79)(79) mod 91 = 6241 mod 91 = 53
Third letter “D” = 4
Transmitted
Sent ciphertext Received
message message
Encrypt Decrypt
2. Create a secret five-letter word (Ex. “CRATE”, “LOVED”, “FLUKE”). Using RSA
Encryption, encrypt the secret word letter-by-letter (A = 1, B = 2, C = 3, …, Z = 26).
Your output will be an encrypted message, which are five numbers corresponding to
each letter.
3. Write your group’s encrypted message on the board. The other groups will decrypt it.
Group For keys (n,e) (n,d) Encrypted Original message
Use (91,5) (91,29) message t c
Or (91,7) (91,31) (visible to all) (answer will be
Or (99,7) (99,23) DON’T USE revealed later)
1 (n,e) = (n,d) =
2 (n,e) = (n,d) =
3 (n,e) = (n,d) =
4 (n,e) = (n,d) =
5 (n,e) = (n,d) =
6 (n,e) = (n,d) =
7 (n,e) = (n,d) =
8 (n,e) = (n,d) =
9 (n,e) = (n,d) =
10 (n,e) = (n,d) =
11 (n,e) = (n,d) =
12 (n,e) = (n,d) =
13 (n,e) = (n,d) =
14 (n,e) = (n,d) =
15 (n,e) = (n,d) =
4. Using RSA Decryption, decrypt the other groups’ secret messages. How many
messages were you able to decrypt?
Sums and their Properties
After simplifying the formula of the variance, we demonstrate that the mean and the variance can
be computed using a single pass over the data instead of two passes. The following Java
program demonstrates the significant speed improvement.
package meanvariance2;
import java.util.Random;
int n = data.length;
int d;
double diff;
double sum;
double sum2;
double mean;
double variance;
long startTime;
long endTime;
long elapsedTime;
startTime = System.currentTimeMillis();
// First Method (fetch the data twice)
sum = 0;
for (i = 0; i < n; i++) {
sum += data[i];
}
mean = sum / n;
sum2 = 0; // sum(x[i] - mean)^2
for (i = 0; i < n; i++) {
diff = data[i] - mean;
sum2 += diff*diff;
}
endTime = System.currentTimeMillis();
System.out.printf("The mean of %d data is %1.4f.\n",n,mean);
variance = sum2 / n;
System.out.printf("The variance of %d data is %1.4f.\n",n,variance);
elapsedTime = endTime - startTime;
System.out.printf("The elapsed time is %d ms.\n",elapsedTime);
// Second method
System.out.printf("Now using the second method....\n");
sum = 0;
sum2 = 0;
startTime = System.currentTimeMillis();
for (i = 0; i < n; i++) {
d = data[i];
sum += d;
sum2 += d * d;
}
endTime = System.currentTimeMillis();
mean = sum / n;
variance = (sum2 - (sum*sum)/n) / n;
System.out.printf("The mean of %d data is %1.4f.\n",n,mean);
System.out.printf("The variance of %d data is %1.4f.\n",n,variance);
elapsedTime = endTime - startTime;
System.out.printf("The elapsed time is %d ms.\n",elapsedTime);
}
}
run:
The mean of 100000000 data is 59.5011.
The variance of 100000000 data is 533.2402.
The elapsed time is 343 ms.
Now using the second method....
The mean of 100000000 data is 59.5011.
The variance of 100000000 data is 533.2402.
The elapsed time is 271 ms. (Second method is much faster!)
MATH2081 Math for Computing November 10, 2017
Name: ___________________________________ S-number: ____________________
Properties of Sums
A data set has the following values and frequencies:
Mean = ___________________
Variance = __________________
● The floor function assigns to the real number x the largest integer that is less than or
equal to x. The value of the floor function at x is denoted by⌊𝑥⌋. Draw the floor function.
How do you make a round() function using the floor function? Draw the round function.
● The ceiling function assigns to the real number x the smallest integer that is greater than
or equal to x. The value of the ceiling function at x is denoted by ⌈ x⌉ . Draw the ceiling
function.
The sector is the minimum storage unit of a hard disk. The operating system allocates a number
of sectors that are enough to store the file. What is the actual disk space consumed by a file
given the hard disk sector size?
File size in Actual disk space Actual disk space Actual disk space
bytes consumed in bytes consumed in bytes consumed in bytes
(sector = 512 bytes) (sector = 2048 bytes) (sector = 4096 bytes)
15,520,234
4,993,294
NOTE: A larger sector size will make hard disk access faster. But for many small files, there will
be a lot of wasted space.
59. How many bytes are required to encode n bits of data where n equals
a) 100 ?
b) 183 ?
c) 1,321 ?
d) 39,827 ?
In asynchronous transfer mode (ATM) (a communications protocol used on backbone networks),
data are organized into cells of 53 bytes. How many complete ATM cells can be transmitted in 1
minute over a connection that transmits data at the rate of 500 kilobits per second?
One cell is 53 bytes = 8 * 53 bits = 424 bits.
Every second, the connection can transmit 500,000 bits.
In one minute (60 seconds), the connection can transmit
60 x 500,000 bits = 30,000,000 bits
In one minute, floor(30,000,000 / 424) complete ATM cells can be transmitted.
60. How many complete ATM cells can be transmitted in 10 seconds over a link operating at the
following rates?
a) 256 kilobits per second (1 kilobit = 1000 bits)
61. Data are transmitted over a particular Ethernet network in blocks of 1500 octets (blocks of 8
bits, same as “byte”). How many blocks are required to transmit the following amounts of data
over this Ethernet network? (Note that a byte is a synonym for an octet, a kilobyte is 1,000 bytes,
and a megabyte is 1,000,000 bytes.)
A JPEG file stores image data in blocks of 8 x 8 pixels. How many blocks are needed to
store a JPEG image whose dimensions are 4653 x 3217 pixels?
A Demonstration of Recursion
Below is a Python code for computing the factorial and the N-th Fibonacci
number:
def main():
n = int(input("Enter an integer greater than 1: "))
print(str(n) + "! = ", factorial(n))
print("The " + str(n) + "-th Fibonacci number is ", fib(n))
def fib(n):
if n == 1 or n == 2: # Base case
return 1
else:
return fib(n-2) + fib(n-1) # Recursive case
def factorial(n):
if n == 1: # base case
return 1
else:
return n * factorial(n-1) # Recursive
# it called itself
def drawSquare(myTurtle,x1,y1,width,color):
myTurtle.up()
myTurtle.goto(x1,y1)
myTurtle.down()
myTurtle.color(color)
myTurtle.shape("turtle")
myTurtle.setheading(0)
for i in range(4):
myTurtle.forward(width)
myTurtle.left(90)
window = turtle.Screen()
joe = turtle.Turtle()
joe.speed(100)
drawSierpinskiCarpet(joe,700,-350,-350)
window.exitonclick()