You are on page 1of 23

Course Journal of MATH 2081 (Math for Computing)

2017 Semester 3

Welcome to Mathematics for Computing!

Mathematics for Computing introduces and studies (with an emphasis on problem


solving) many of the fundamental ideas and methods of discrete mathematics that are
the tools of the computer scientist. The course demonstrates the importance of discrete
mathematics for computer science.

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)

Example of Encryption and Decryption


Weekly Activity (November 3, Early Feedback)

Sums and Their Properties: A program demonstration


A Demonstration of Recursion Using A Program in Python
The Teacher
● Vladimir “Vlad” Mariano, Ph.D. (his CV)
● Graduated in 2003 with a PhD in Computer Science and Engineering at
Pennsylvania State University (Penn State).
● Email: vladimir.mariano@rmit.edu.vn
● Consultation hours: Wednesdays 10:30-1:30pm at 2.4.27

Course Learning Outcomes (CLO)


On completion of this course you should be able to:
1. Identify and apply basic concepts of set theory, arithmetic, logic, proof techniques,
binary relations, graphs and trees, counting methods and probability.
2. Produce convincing arguments, conceive and/or analyse basic mathematical
proofs and discriminate between valid and fallacious arguments.
3. Apply the knowledge and skills obtained to investigate and solve a variety of
discrete mathematical problems
4. Communicate both technical and non-technical material in a range of forms
(written, oral, electronic, graphic).
5. Demonstrate effective use of appropriate technology.

Topics with an emphasis on its applications in computing:


● Logic
● Arithmetic
● Proof Techniques
● Binary Relations
● Set Theory
● Counting Methods
● Discrete Probability
● Graphs
● Trees

Reference Book:
Kenneth Rosen, “Discrete Mathematics and Its Applications”, 7th Edition
Assessment:

● Weekly Activity 25%. This assessment supports CLOs 1,2,3,4,5. This


assessment is given once a week. The activity could be a quiz or a group
exercise to reinforce their learning of mathematical concepts.
● Week 7: Personal Development Week. During this week, you will watch a movie
“The Imitation Game” and reflect on the role of mathematics (particularly
cryptography) on the turn of world history. There will be an online discussion
during this week which will be assessed as part of the Weekly Activity.
● Class Test 1 25%
● Class Test 2 25%
● Class Test 3 25%
MATH 2081 Math for Computing October 20, 2017 Weekly Activity (Early Feedback)

Name: ________________________________ S-number: _________________

Fill in the truth values of the following


P Q P and Q P or Q P→Q P↔Q P xor Q

T T

T F

F T

F F

What are the truth values of these statements?


P Q (P ↔ Q) → (P xor Q) (P → ~Q) and (Q → ~P) (P ↔ Q) xor (P ↔ ~Q)

T T

T F

F T

F F

What are the truth values of these statements?


P Q R (P → ~Q) or (~P → R) (P ↔ Q) xor (~P ↔ ~R) ~P and Q and ~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
Logic in Programming

The following is a Java program for saying an amount in Vietnamese


package sayviet1;
import java.util.Scanner;

public class SayViet1 {

private static String sayDigit(int digit) { // say one digit


String [] digitName =
{"khong","mot","hai","ba","bon","nam","sau","bay","tam","chin"};
return digitName[digit];
}

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;
}

private static String sayAmount(int amount) {


int millions;
int thousands;
int ones;
millions = amount / 1000000;
amount = amount - millions * 1000000;
thousands = amount / 1000;
amount = amount - thousands * 1000;
ones = amount;
String viet = "";
if (millions > 0) {
viet = viet + " " + sayNumber( 0,millions ) + " trieu";
}
if (thousands > 0) {
viet = viet + " " + sayNumber( millions,thousands) + " nghin";
}
if (ones > 0) {
viet = viet + " " + sayNumber( millions + thousands,ones);
}
return viet;
}

public static void main(String[] args) {


Scanner scan1 = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scan1.nextInt();
System.out.println();
System.out.println("The number is " + number);
System.out.println("In Vietnamese, this is " + sayAmount(number));
}
}
The last column in the Activity comes from the “le” rule condition in the way an amount
spoken in Vietnamese:

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";
}

Example P Q R ~P and Q and ~R


Hundreds Tens is Ones is → Say “le” in between the
is zero zero zero hundred and ones digit

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)

Name: ________________________________ S-number: _________________

1. Convert the decimal number 186 to 8-bit binary. 186 = ____________________ (binary)

2. Convert the binary number 01111011 to decimal. 01111011 = ____________ (decimal)

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

A B Ci Co = (Ci and (A xor B)) or (A and B) S = (A xor B) xor Ci


0 0 0 0 0

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

What does this circuit compute?


This circuit computes the sum of A, B and Ci. S is the sum, Co is the carry out.
5. Draw the truth table of the 7-segment LED digital display circuit

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

7. How do you check if a circle K is completely inside a rectangle G? Circle K has a


center (x,y) and a radius r. The rectangle G is defined by two corner points (x1,y1) and
(x2,y2).

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).

if (~(G1 does not overlap G2)){


Then G1 has collided with G2
}
if (~(x2<x3 or x1>x4 or y2<y3 or y1>y4)){
Then G1 has collided with G2
}

HINT 1: When G1 and G2 are overlapping,


x2 >= x3 always
Encryption and Decryption

Example: Transmit the word “BED”, which is “B” = 2, “E” = 5, “D” = 4

First letter “B” = 2

Transmitted
Sent ciphertext Received
message message
Encrypt Decrypt

Public keys: Private keys:


n=91, e=5 n=91, d=29

t = c^e mod n r = t^d mod n


= 2^5 mod 91 = 32^29 mod 91
= 32 mod 91 =2
= 32

32^29 mod 91 = 32^(16+8+4+1) mod 91


= (32^16)(32^8)(32^4)(32) mod 91
= (32^16 mod 91)(32^8 mod 91)(32^4 mod 91)(32 mod 91) mod 91
= (74) (16) (74) (32) mod 91
= 2

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

Public keys: Private keys:


n=91, e=5 n=91, d=29

t = c^e mod n r = t^d mod n


= 5^5 mod 91 = 31^29 mod 91
= 3125 mod 91 =5
= 31
31^29 mod 91 = 31^(16+8+4+1) mod 91
= (31^16)(31^8)(31^4)(31) mod 91
= (31^16 mod 91)(31^8 mod 91)(31^4 mod 91)(31 mod 91) mod 91
= (53) (79) (53) (31) mod 91
= 5

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

Public keys: Private keys:


n=91, e=5 n=91, d=29

t = c^e mod n r = t^d mod n


= 4^5 mod 91 = 23^29 mod 91
= 1024 mod 91 =4
= 23

23^29 mod 91 = 23^(16+8+4+1) mod 91 “exponent is divided into powers of 2”


= (23^16)(23^8)(23^4)(23) mod 91
= (23^16 mod 91)(23^8 mod 91)(23^4 mod 91)(23 mod 91) mod 91
= 16 x 74 x 16 x 23 mod 91
= 4
where
23^1 mod 91 = 23
23^2 mod 91 = 529 mod 91 = 74
23^4 mod 91 = (23^2)(23^2) mod 91
= (23^2 mod 91)(23^2 mod 91) mod 91
= (74)(74) mod 91 = 5476 mod 91= 16
23^8 mod 91 = (23^4)(23^4) mod 91
= (23^4 mod 91)(23^4 mod 91) mod 91
= (16)(16) mod 91 = 256 mod 91= 74
23^16 mod 91 = (23^8)(23^8) mod 91
= (23^8 mod 91)(23^8 mod 91) mod 91
= (74)(74) mod 91 = 5476 mod 91 = 16
Encryption and Decryption Weekly Activity (November 3, 2017, Early Feedback)
Name: __________________________ S-number: ____________
Name: __________________________ S-number: ____________

1. Form a group of two persons.

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;

public class MeanVariance2 {

public static void main(String[] args) {


Random rand1 = new Random();
int [] data = new int[100000000];
int i;

for (i = 0; i < data.length; i++) {


data[i] = 20 + rand1.nextInt(80); // [20..99]
}

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);
}
}

Here is the output:

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:

Value 6 7 8 9 10 11 Total (N)

Frequency 330 476 428 296 455 120 2105


Compute the mean and the variance using the formulas we derived.

Mean = ___________________

Variance = __________________

Ceiling and Floor functions

● 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.

How do you make a round() function using the ceiling function?


Examples:

How many bytes are needed to send 300 bits of information?

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

41,981 41,981 bytes would


occupy
ceiling(41,981/512) =
ceiling(81.994) = 82
sectors. 82 sectors
occupy 82*512 =
41,984 bytes on the
hard disk.

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.

From Rosen Book, Chapter 2.3, page 155, #59 to #61

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)

b) 360 kilobits per second

c) 2 megabits per second (1 megabit = 1,000,000 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) 251 kilobytes of data

b) 784 kilobytes of data

c) 1.654 megabytes of data

d) 48.3 megabytes of data

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:

# Recursive Functions Using Python


# 1. Compute the factorial of a number using a recursive function
# Factorial is defined as n! = n*(n-1)*(n-2) * ... * 3 * 2 * 1
# 2. Compute the n-th Fibonacci number in the sequence: 1,1,2,3,5,
# 8, 13, 21, 34,...
# Recursively, the n-th Fibonacci number is
# fib(1)=fib(2)=1, fib(n) = fib(n-1) + fib(n-2)

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

if __name__ == '__main__': main()

How does the running time grow with N ?


import turtle

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)

def drawSierpinskiCarpet(myTurtle,width, x, y):


if width < 3:
return
else:
drawSquare(myTurtle, x + width/3, y + width/3, width/3, "red")
for i in range(3):
for j in range(3):
if not (i==1 and j==1): # draw 8 smaller carpets
drawSierpinskiCarpet(myTurtle,width/3,x+i*width/3,y+j*width/3)

window = turtle.Screen()
joe = turtle.Turtle()
joe.speed(100)
drawSierpinskiCarpet(joe,700,-350,-350)
window.exitonclick()

You might also like