Docsity

Discrete Structures - Math for Computer Science - Lecture Slides | CSCI 1900, Study notes of Computer Science

Toggle sidebar
bg1
1
Integers – Page 1CSCI 1900 – Discrete Structures
CSCI 1900
Discrete Structures
Integers
Reading: Kolman, Section 1.4
Integers – Page 2CSCI 1900 – Discrete Structures
Divisibility
• If one integer, n, divides into a second
integer, m, without producing a remainder,
then we say that “n divides m”.
• Denoted n | m
• If one integer, n, does not divide evenly
into a second integer, m, i.e., m÷n
produces a remainder, then we say that “n
does not divide m”
• Denoted n | m
Integers – Page 3CSCI 1900 – Discrete Structures
Some Properties of Divisibility
• If n | m, then there exists a q such that m = q×n
• The absolute values of both q and n are less than
the absolute value of m, i.e., |n| < |m| and |q| < |m|
• Examples:
4 | 24: 24 = 4×6 and both 4 and 6 are less than 24.
5 | 135: 135 = 5×27 and both 5 and 27 are less than 135
• Simple properties of divisibility (proofs on page 21)
– If a | b and a | c, then a | (b + c)
– If a | b and a | c, where b > c, then a | (b - c)
– If a | b or a | c, then a | bc
– If a | b and b | c, then a | c
Integers – Page 4CSCI 1900 – Discrete Structures
Prime Numbers
• A number pis called prime if the only
positive integers that divide pare pand 1.
• Examples of prime numbers: 2, 3, 5, 7, 11,
and 13.
• There is a science to determining prime
numbers. The following slides present
some computer algorithms that can be
used to determine if a number n>1 is
prime.
Integers – Page 5CSCI 1900 – Discrete Structures
Basic Primer Number Algorithm
1. First, check if n=2. If it is, n is prime.
Otherwise, proceed to step 2.
2. Check to see if each integer k is a divisor
of n where 1<k<(n-1). If none of the
values of k are divisors of n, then n is
prime
Integers – Page 6CSCI 1900 – Discrete Structures
Better Prime Number Algorithm
Note that if n=mk, then either m or k is less
than √n. Therefore, we don't need to check
for values of k greater than √n.
1. First check if n=2. If it is, n is prime. Otherwise,
proceed to step 2.
2. Check to see if each integer k is a divisor of n
where 1<k<√n. If none of the values of k are
divisors of n, then n is prime
pf3
pf4
pf5

Partial preview of the text

Download Discrete Structures - Math for Computer Science - Lecture Slides | CSCI 1900 and more Study notes Computer Science in PDF only on Docsity!

CSCI 1900 – Discrete Structures Integers – Page 1

CSCI 1900

Discrete Structures

Integers

Reading: Kolman, Section 1.

CSCI 1900 – Discrete Structures Integers – Page 2

Divisibility

  • If one integer, n, divides into a second

integer, m, without producing a remainder,

then we say that “n divides m”.

  • Denoted n | m
  • If one integer, n, does not divide evenly

into a second integer, m, i.e., m÷n

produces a remainder, then we say that “n

does not divide m”

  • Denoted n | m

CSCI 1900 – Discrete Structures Integers – Page 3

Some Properties of Divisibility

  • If n | m, then there exists a q such that m = q×n
  • The absolute values of both q and n are less than the absolute value of m, i.e., |n| < |m| and |q| < |m|
  • Examples: 4 | 24: 24 = 4×6 and both 4 and 6 are less than 24. 5 | 135: 135 = 5×27 and both 5 and 27 are less than 135
  • Simple properties of divisibility (proofs on page 21)
    • If a | b and a | c, then a | (b + c)
    • If a | b and a | c, where b > c, then a | (b - c)
    • If a | b or a | c, then a | bc
    • If a | b and b | c, then a | c

CSCI 1900 – Discrete Structures Integers – Page 4

Prime Numbers

  • A number p is called prime if the only

positive integers that divide p are p and 1.

  • Examples of prime numbers: 2, 3, 5, 7, 11,

and 13.

  • There is a science to determining prime

numbers. The following slides present

some computer algorithms that can be

used to determine if a number n>1 is

prime.

CSCI 1900 – Discrete Structures Integers – Page 5

Basic Primer Number Algorithm

1. First, check if n=2. If it is, n is prime.

Otherwise, proceed to step 2.

2. Check to see if each integer k is a divisor

of n where 1<k<(n-1). If none of the

values of k are divisors of n, then n is

prime

CSCI 1900 – Discrete Structures Integers – Page 6

Better Prime Number Algorithm

Note that if n=mk, then either m or k is less

than √n. Therefore, we don't need to check

for values of k greater than √n.

  1. First check if n=2. If it is, n is prime. Otherwise, proceed to step 2.
  2. Check to see if each integer k is a divisor of n where 1<k<√n. If none of the values of k are divisors of n, then n is prime

CSCI 1900 – Discrete Structures Integers – Page 7

Even Better Prime Number Algorithm

Note that if k | n, and k is even, then 2 | n.

Therefore, if 2 does not divide n, then no even

number can be a divisor of n. (If a | b and b | c,

then a | c)

  1. First check if n=2. If it is, n is prime. Otherwise, proceed to step 2.
  2. Check if 2 | n. If so, n is not prime. Otherwise, proceed to step 3.
  3. Check to see if each odd integer k is a divisor of n where 1<k<√n. If none of the values of k are divisors of n, then n is prime.

CSCI 1900 – Discrete Structures Integers – Page 8

Even 2 Better Prime Number Algorithm

Note that if k | n, and d | k, then d | n.

Therefore, if d does not divide n, then no

multiple of d can be a divisor of n.

1. First check if n=2. If it is, n is prime.

Otherwise, proceed to step 2.

2. Use a sequence k = 2, 3, 5, 7, 11, 13, 17,

… up to √n to check if k | n. If none are the

values of k are divisors of n, then n is

prime. (Note that list is a list of prime

numbers!)

CSCI 1900 – Discrete Structures Integers – Page 9

Factoring a Number into its Primes

  • Dividing a number into its multiples over and over again until the multiples cannot be divided any longer shows us that any number can eventually be broken down into prime numbers.
  • Examples: 9 = 3⋅3 = 3^2 24 = 8⋅3 = 2⋅ 2 ⋅ 2 ⋅3 = 2^3 ⋅ 3 315 = 3⋅105 = 3⋅ 3 ⋅35 = 3⋅ 3 ⋅ 5 ⋅7 = 3^2 ⋅ 5 ⋅ 7
  • Basically, this means that any number can be broken into multiples of prime numbers.

CSCI 1900 – Discrete Structures Integers – Page 10

Factoring into Primes (continued)

Each row of the table below presents a different number factored into its primes. The numbers in the columns represent the number of each particular prime can be factored out of each original value.

CSCI 1900 – Discrete Structures Integers – Page 11

Factoring into Primes (continued)

  • Every positive integer n > 1 can be broken

into multiples of prime numbers.

  • n = p 1 k1p 2 k2p 3 k3p 4 k4^ …psks

p 1 < p 2 < p 3 < p 4 < …< ps

CSCI 1900 – Discrete Structures Integers – Page 12

Methods for Factoring

  • 2 | n Æ If least significant digit of n is divisible

by 2 (i.e., n is even), then 2 divides n

  • 3 | n Æ If the sum of all the digits of n down

to a single digit equals 3, 6, or 9, then 3

divides n. For example, is 17,587,

divisible by 3?

1 + 2 = 3 Æ YES! 3 divides 17,587,

CSCI 1900 – Discrete Structures Integers – Page 19

Examples of m = qn + r

  • If n is 3 and m is 16, then 16 = 5(3) + 1 so

q = 5 and r = 1

  • If n is 10 and m is 3, then 3 = 0(10) + 3 so

q = 0 and r = 3

  • If n is 5 and m is –11, then – 11 = – 3(5) +

4 so q = – 3 and r = 4

CSCI 1900 – Discrete Structures Integers – Page 20

Greatest Common Divisor

  • If a, b, and k are in Z+, and k | a and k | b,

we say that k is a common divisor.

  • If d is the largest such k, d is called the

greatest common divisor (GCD).

  • d is a multiple of every k, i.e., every k

divides d.

CSCI 1900 – Discrete Structures Integers – Page 21

GCD Example

Find the GCD of 540 and 315:

  • 540 = 2^2 ⋅ 33 ⋅ 5
  • 315 = 3^2 ⋅ 5 ⋅ 7
  • 540 and 315 share the divisors 3, 3 2 , 5, 3⋅5, and 32 ⋅5 (Look at it as the number of possible ways to combine 3, 3, and 5)
  • The largest is the GCD Æ 32 ⋅5 = 45
  • 315 ÷45 = 7 and 540÷45=

CSCI 1900 – Discrete Structures Integers – Page 22

Theorems of the GCD

Assume d is GCD(a, b)

  • d = sa + tb for some integers s and t. (s and t are not necessarily positive.)
  • If c is any other common divisor of a and b, then c | d
  • If d is the GCD(a, b), then d | a and d | b
  • Assume d is the GCD(a, b). If c | a and c | b, then c | d
  • There is a horrendous proof of these theorems on page 22 of our textbook. You are not responsible for this proof!

CSCI 1900 – Discrete Structures Integers – Page 23

GCD Theorem

  • If a and b are in Z +, a>b, then GCD(a,b) = GCD(a, a+b)
  • If c divides a and b, it divides a+b (this is from the earlier “divides” theorems)
  • Since b = a-(a-b) = -a+(a+b), then a common divisor of a and (a+b) also divides a and b
  • Since all c that divide a or b must also divide b and b+a, then they have the same complete set of divisors and therefore the same GCD.

CSCI 1900 – Discrete Structures Integers – Page 24

Euclidean Algorithm

  • The Euclidean Algorithm is a recursive algorithm that can be used to find GCD (a, b)
  • It is based on the fact that for any two integers, a > b, there exists a k and r such that:

a = k⋅b + r

  • Since if a | b and a | c, then a | (b + c), then we know that the GCD (a,b) must also divide r. Therefore, the GCD (a,b) = GCD(b,r)

CSCI 1900 – Discrete Structures Integers – Page 25

Euclidean Algorithm Process

  • For two integers a and b where a > b > 0 a = k 1 b + r 1 , where k 1 is in Z+ and 0 < r 1 < b
  • If r 1 = 0, then b | a and b the is GCD(a, b)
  • If r 1 ≠ 0, then if some integer n divides a and b, then it must also divide r 1. Similarly, if n divides b and r 1 , then it must divide a.
  • Go back to top substituting b for a and r 1 for b. Repeat until r (^) n = 0 and k (^) n will be GCD

CSCI 1900 – Discrete Structures Integers – Page 26

Least Common Multiple

  • If a, b, and k are in Z+, and a | k, b | k, we say that k is a common multiple of a and b.
  • The smallest such k, call it c, is called the least common multiple or LCM of a and b
  • We write c = LCM(a,b)

CSCI 1900 – Discrete Structures Integers – Page 27

Deriving the LCM

  • We can obtain LCM from a, b, and GCD(a,b)
  • For any integers a and b, we can write a = p 1 a p 2 a2^ …pkak^ and b = p 1 b1^ p 2 b2^ …pkbk
  • GCD(a,b) = p 1 min(a1,b1)^ p 2 min(a2,b2)^ …pkmin(ak,bk)
  • LCM(a,b) = p 1 max(a1,b1)^ p 2 max(a2,b2)^ …pkmax(ak,bk)
  • Since, GCD(a,b)⋅LCM(a,b) = p 1 (a1+b1)^ p 2 (a2+b2) …pk(ak+bk) = p 1 a1^ p 1 b1^ p 2 a2^ p 2 b2^ …pkak^ pkbk = a⋅b
  • Therefore, LCM(a,b) = a⋅b/GCD(a,b)

CSCI 1900 – Discrete Structures Integers – Page 28

Mod-n function

  • If z is a nonnegative integer, the mod-n function, fn (z), is defined as f (^) n (z) = r if z = qn + r
  • For example: f 3 (14) = 2 because 14 = 4⋅3 + 2 f 7 (153) = 6 because 153 = 21⋅7 + 6

CSCI 1900 – Discrete Structures Integers – Page 29

Representation of integers

  • We are used to decimal, but in reality, it is only one of many ways to describe an integer
  • We say that a decimal value is the “base 10 expansion of n” or the “decimal expansion of n”
  • If b>1 is an integer, then every positive integer n can be uniquely expressed in the form: n = dkbk^ + dk-1 bk-1^ + dk-2 bk-2^ + … + d 1 b^1 + d 0 b^0 where 0 < di < b, i = 0, 1, …, k

CSCI 1900 – Discrete Structures Integers – Page 30

Proof that There is Exactly One Base Expansion

  • Proof is on bottom of page 27
  • Basis of proof is that n = dkbk^ + r
  • If dk > bk, then k was not the largest non- negative integer so that bk^ < n.
  • If r > bk, then dk isn’t large enough
  • Go back to 1 replacing n with r. This time, remember that k = k-1, because r must be less than bk
  • Repeat until k=0.