37
Fall 2002 CMSC 203 - Discrete Structures 1 Let us get into… Let us get into… Number Theory Number Theory

Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Embed Size (px)

Citation preview

Page 1: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 1

Let us get into…Let us get into…

Number TheoryNumber Theory

Page 2: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 2

Introduction to Number TheoryIntroduction to Number Theory

Number theory is about Number theory is about integersintegers and their and their properties.properties.

We will start with the basic principles ofWe will start with the basic principles of

• divisibility,divisibility,• greatest common divisors,greatest common divisors,• least common multiples, andleast common multiples, and• modular arithmeticmodular arithmetic

and look at some relevant algorithms. and look at some relevant algorithms.

Page 3: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 3

DivisionDivision

If a and b are integers with a If a and b are integers with a 0, we say that 0, we say that a a dividesdivides b if there is an integer c so that b = b if there is an integer c so that b = ac.ac.

When a divides b we say that a is a When a divides b we say that a is a factorfactor of b of b and that b is a and that b is a multiplemultiple of a. of a.

The notation The notation a | ba | b means that a divides b. means that a divides b.

We write We write a X ba X b when a does not divide b when a does not divide b(see book for correct symbol).(see book for correct symbol).

Page 4: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 4

Divisibility TheoremsDivisibility Theorems

For integers a, b, and c it is true thatFor integers a, b, and c it is true that

• if a | b and a | c, then a | (b + c)if a | b and a | c, then a | (b + c) Example:Example: 3 | 6 and 3 | 9, so 3 | 15. 3 | 6 and 3 | 9, so 3 | 15.

• if a | b, then a | bc for all integers cif a | b, then a | bc for all integers c Example:Example: 5 | 10, so 5 | 20, 5 | 30, 5 | 40, … 5 | 10, so 5 | 20, 5 | 30, 5 | 40, …

• if a | b and b | c, then a | cif a | b and b | c, then a | c Example:Example: 4 | 8 and 8 | 24, so 4 | 24. 4 | 8 and 8 | 24, so 4 | 24.

Page 5: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 5

PrimesPrimes

A positive integer p greater than 1 is called A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and prime if the only positive factors of p are 1 and p.p.

A positive integer that is greater than 1 and is A positive integer that is greater than 1 and is not prime is called composite.not prime is called composite.

The fundamental theorem of arithmetic:The fundamental theorem of arithmetic:

Every positive integer can be written Every positive integer can be written uniquelyuniquely as the as the product of primesproduct of primes, where the prime , where the prime factors are written in order of increasing size.factors are written in order of increasing size.

Page 6: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 6

PrimesPrimes

Examples:Examples:

3·53·5

48 =48 =

17 =17 =

100 100 ==512 512 ==515 515 ==28 =28 =

15 =15 =

2·2·2·2·3 = 22·2·2·2·3 = 244·3·3

1717

2·2·5·5 = 22·2·5·5 = 222·5·522

2·2·2·2·2·2·2·2·2 = 22·2·2·2·2·2·2·2·2 = 299

5·1035·103

2·2·72·2·7

Page 7: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 7

PrimesPrimes

If n is a composite integer, then n has a prime If n is a composite integer, then n has a prime divisor less than or equal .divisor less than or equal .

This is easy to see: if n is a composite integer, This is easy to see: if n is a composite integer, it must have two prime divisors pit must have two prime divisors p11 and p and p22 such such that pthat p11pp22 = n. = n.

pp11 and p and p22 cannot both be greater than cannot both be greater than , because then p, because then p11pp22 > n. > n.

n

n

Page 8: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 8

The Division AlgorithmThe Division Algorithm

Let Let aa be an integer and be an integer and dd a positive integer. a positive integer.Then there are unique integers Then there are unique integers qq and and rr, with , with 0 0 r < d r < d, such that , such that a = dq + ra = dq + r..

In the above equation, In the above equation, • dd is called the divisor, is called the divisor, • aa is called the dividend, is called the dividend, • qq is called the quotient, and is called the quotient, and • rr is called the remainder. is called the remainder.

Page 9: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 9

The Division AlgorithmThe Division Algorithm

Example:Example:

When we divide 17 by 5, we haveWhen we divide 17 by 5, we have

17 = 517 = 53 + 2.3 + 2.

• 17 is the dividend,17 is the dividend,• 5 is the divisor,5 is the divisor,• 3 is called the quotient, and3 is called the quotient, and• 2 is called the remainder.2 is called the remainder.

Page 10: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 10

The Division AlgorithmThe Division Algorithm

Another example:Another example:

What happens when we divide -11 by 3 ?What happens when we divide -11 by 3 ?

Note that the remainder cannot be negative.Note that the remainder cannot be negative.

-11 = 3-11 = 3(-4) + 1.(-4) + 1.

• -11 is the dividend,-11 is the dividend,• 3 is the divisor,3 is the divisor,• -4 is called the quotient, and-4 is called the quotient, and• 1 is called the remainder.1 is called the remainder.

Page 11: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 11

Greatest Common DivisorsGreatest Common DivisorsLet a and b be integers, not both zero.Let a and b be integers, not both zero.The largest integer d such that d | a and d | b is The largest integer d such that d | a and d | b is called the called the greatest common divisorgreatest common divisor of a and b. of a and b.The greatest common divisor of a and b is denoted The greatest common divisor of a and b is denoted by gcd(a, b).by gcd(a, b).

Example 1:Example 1: What is gcd(48, 72) ? What is gcd(48, 72) ?The positive common divisors of 48 and 72 are The positive common divisors of 48 and 72 are 1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24. 1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24.

Example 2:Example 2: What is gcd(19, 72) ? What is gcd(19, 72) ?The only positive common divisor of 19 and 72 isThe only positive common divisor of 19 and 72 is1, so gcd(19, 72) = 1. 1, so gcd(19, 72) = 1.

Page 12: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 12

Greatest Common DivisorsGreatest Common Divisors

Using prime factorizations:Using prime factorizations:

a = pa = p11aa1 1 p p22

aa2 2 … p… pnnaan n , b = p, b = p11

bb1 1 p p22bb2 2 … p… pnn

bbn n ,,

where pwhere p11 < p < p22 < … < p < … < pnn and a and aii, b, bii NN for 1 for 1 i i n n

gcd(a, b) = pgcd(a, b) = p11min(amin(a11, b, b1 1 )) p p22

min(amin(a22, b, b2 2 )) … p… pnnmin(amin(ann, b, bn n ))

Example:Example:

a = 60 a = 60 = =

2222 3 311 5 511

b = 54 b = 54 = =

2211 3 333 5 500

gcd(a, b) gcd(a, b) = =

2211 3 311 5 50 0 = 6 = 6

Page 13: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 13

Relatively Prime IntegersRelatively Prime IntegersDefinition:Definition:

Two integers a and b are Two integers a and b are relatively primerelatively prime if if gcd(a, b) = 1.gcd(a, b) = 1.

Examples:Examples:

Are 15 and 28 relatively prime?Are 15 and 28 relatively prime?Yes, gcd(15, 28) = 1.Yes, gcd(15, 28) = 1.Are 55 and 28 relatively prime?Are 55 and 28 relatively prime?Yes, gcd(55, 28) = 1.Yes, gcd(55, 28) = 1.Are 35 and 28 relatively prime?Are 35 and 28 relatively prime?No, gcd(35, 28) = 7.No, gcd(35, 28) = 7.

Page 14: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 14

Relatively Prime IntegersRelatively Prime Integers

Definition:Definition:

The integers aThe integers a11, a, a22, …, a, …, ann are are pairwise pairwise

relatively primerelatively prime if gcd(a if gcd(aii, a, ajj) = 1 whenever 1 ) = 1 whenever 1

i < j i < j n. n.

Examples:Examples:

Are 15, 17, and 27 pairwise relatively prime?Are 15, 17, and 27 pairwise relatively prime?No, because gcd(15, 27) = 3.No, because gcd(15, 27) = 3.

Are 15, 17, and 28 pairwise relatively prime?Are 15, 17, and 28 pairwise relatively prime?Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 and gcd(17, 28) = 1.and gcd(17, 28) = 1.

Page 15: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 15

Least Common MultiplesLeast Common MultiplesDefinition:Definition:

The The least common multipleleast common multiple of the positive of the positive integers a and b is the smallest positive integers a and b is the smallest positive integer that is divisible by both a and b.integer that is divisible by both a and b.

We denote the least common multiple of a and We denote the least common multiple of a and b by lcm(a, b).b by lcm(a, b).

Examples:Examples:

lcm(3, 7) lcm(3, 7) ==

2121

lcm(4, 6) lcm(4, 6) ==

1212

lcm(5, 10) lcm(5, 10) ==

1010

Page 16: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 16

Least Common MultiplesLeast Common Multiples

Using prime factorizations:Using prime factorizations:

a = pa = p11aa1 1 p p22

aa2 2 … p… pnnaan n , b = p, b = p11

bb1 1 p p22bb2 2 … p… pnn

bbn n ,,

where pwhere p11 < p < p22 < … < p < … < pnn and a and aii, b, bii NN for 1 for 1 i i n n

lcm(a, b) = plcm(a, b) = p11max(amax(a11, b, b1 1 )) p p22

max(amax(a22, b, b2 2 )) … p… pnnmax(amax(ann, b, bn n ))

Example:Example:

a = 60 a = 60 = =

2222 3 311 5 511

b = 54 b = 54 = =

2211 3 333 5 500

lcm(a, b) lcm(a, b) = =

2222 3 333 5 51 1 = 4 = 427275 = 5405 = 540

Page 17: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 17

GCD and LCMGCD and LCM

a = 60 a = 60 = =

2222 3 311 5 511

b = 54 b = 54 = =

2211 3 333 5 500

lcm(a, b) lcm(a, b) = =

2222 3 333 5 51 1 = 540 = 540

gcd(a, b) gcd(a, b) = =

2211 3 311 5 50 0 = 6 = 6

Theorem: aTheorem: ab b ==

gcd(a,b)gcd(a,b)lcm(a,lcm(a,b)b)

Page 18: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 18

Modular ArithmeticModular Arithmetic

Let a be an integer and m be a positive integer.Let a be an integer and m be a positive integer.We denote by We denote by a mod ma mod m the remainder when a the remainder when a is divided by m.is divided by m.

Examples:Examples:

9 mod 4 9 mod 4 ==

11

9 mod 3 9 mod 3 ==

00

9 mod 10 9 mod 10 ==

99

-13 mod 4 -13 mod 4 ==

33

Page 19: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 19

CongruencesCongruences

Let a and b be integers and m be a positive Let a and b be integers and m be a positive integer. We say that integer. We say that a is congruent to b a is congruent to b modulo mmodulo m if if m divides a – b.m divides a – b.

We use the notation We use the notation a a b (mod m) b (mod m) to indicate to indicate that a is congruent to b modulo m.that a is congruent to b modulo m.

In other words:In other words:a a b (mod m) if and only if b (mod m) if and only if a mod m = b mod a mod m = b mod mm. .

Page 20: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 20

CongruencesCongruencesExamples:Examples:Is it true that 46 Is it true that 46 68 (mod 11) ? 68 (mod 11) ?Yes, because 11 | (46 – 68).Yes, because 11 | (46 – 68).Is it true that 46 Is it true that 46 68 (mod 22)? 68 (mod 22)?Yes, because 22 | (46 – 68).Yes, because 22 | (46 – 68).For which integers z is it true that z For which integers z is it true that z 12 (mod 12 (mod 10)?10)?It is true for any zIt is true for any z{…,-28, -18, -8, 2, 12, 22, 32, {…,-28, -18, -8, 2, 12, 22, 32, …}…}

Theorem:Theorem: Let m be a positive integer. The Let m be a positive integer. The integers a and b are congruent modulo m if and integers a and b are congruent modulo m if and only if there is an integer k such that a = b + km.only if there is an integer k such that a = b + km.

Page 21: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 21

CongruencesCongruences

Theorem:Theorem: Let m be a positive integer. Let m be a positive integer. If a If a b (mod m) and c b (mod m) and c d (mod m), then d (mod m), then a + c a + c b + d (mod m) and ac b + d (mod m) and ac bd (mod m). bd (mod m).Proof:Proof: We know that a We know that a b (mod m) and c b (mod m) and c d (mod m) d (mod m) implies that there are integers s and t with implies that there are integers s and t with b = a + sm and d = c + tm. b = a + sm and d = c + tm. Therefore,Therefore,b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) andandbd = (a + sm)(c + tm) = ac + m(at + cs + stm).bd = (a + sm)(c + tm) = ac + m(at + cs + stm).Hence, a + c Hence, a + c b + d (mod m) and ac b + d (mod m) and ac bd (mod bd (mod m).m).

Page 22: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 22

The Euclidean Algorithm The Euclidean Algorithm

The The Euclidean AlgorithmEuclidean Algorithm finds the finds the greatest greatest common divisorcommon divisor of two integers a and b. of two integers a and b.

For example, if we want to find gcd(287, 91), For example, if we want to find gcd(287, 91), we we dividedivide 287 by 91: 287 by 91:

287 = 91287 = 913 + 143 + 14

We know that for integers a, b and c,We know that for integers a, b and c,if a | b and a | c, then a | (b + c).if a | b and a | c, then a | (b + c).

Therefore, any divisor of 287 and 91 must also Therefore, any divisor of 287 and 91 must also be a divisor of 287 - 91be a divisor of 287 - 913 = 14.3 = 14.

Consequently, gcd(287, 91) = gcd(14, 91).Consequently, gcd(287, 91) = gcd(14, 91).

Page 23: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 23

The Euclidean Algorithm The Euclidean Algorithm

In the next step, we divide 91 by 14:In the next step, we divide 91 by 14:

91 = 1491 = 146 + 76 + 7

This means that gcd(14, 91) = gcd(14, 7).This means that gcd(14, 91) = gcd(14, 7).

So we divide 14 by 7:So we divide 14 by 7:

14 = 714 = 72 + 02 + 0

We find that 7 | 14, and thus gcd(14, 7) = 7.We find that 7 | 14, and thus gcd(14, 7) = 7.

Therefore, gcd(287, 91) = 7.Therefore, gcd(287, 91) = 7.

Page 24: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 24

The Euclidean Algorithm The Euclidean Algorithm

In In pseudocodepseudocode, the algorithm can be , the algorithm can be implemented as follows: implemented as follows:

procedureprocedure gcd(a, b: positive integers) gcd(a, b: positive integers)x := ax := ay := by := bwhilewhile y y 0 0beginbegin

r := x r := x modmod y yx := yx := yy := ry := r

endend {x is gcd(a, b)}{x is gcd(a, b)}

Page 25: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 25

Representations of IntegersRepresentations of Integers

Let b be a positive integer greater than 1.Let b be a positive integer greater than 1.Then if n is a positive integer, it can be Then if n is a positive integer, it can be expressed expressed uniquelyuniquely in the form: in the form:

n = an = akkbbkk + a + ak-1k-1bbk-1k-1 + … + a + … + a11b + ab + a00,,

where k is a nonnegative integer,where k is a nonnegative integer,aa00, a, a11, …, a, …, akk are nonnegative integers less than b, are nonnegative integers less than b,and aand akk 0. 0.

Example for b=10:Example for b=10:

859 = 8859 = 8101022 + 5 + 5101011 + 9 + 9101000

Page 26: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 26

Representations of IntegersRepresentations of Integers

Example for b=2 (binary expansion):Example for b=2 (binary expansion):

(10110)(10110)22 = 1 = 12244 + 1 + 12222 + 1 + 12211 = (22) = (22)1010

Example for b=16 (hexadecimal Example for b=16 (hexadecimal expansion):expansion):

(we use letters A to F to indicate numbers 10 to (we use letters A to F to indicate numbers 10 to 15)15)

(3A0F)(3A0F)1616 = 3 = 3161633 + 10 + 10161622 + 15 + 15161600 = (14863) = (14863)1010

Page 27: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 27

Representations of IntegersRepresentations of IntegersHow can we construct the base b expansion of an How can we construct the base b expansion of an integer n?integer n?

First, divide n by b to obtain a quotient qFirst, divide n by b to obtain a quotient q00 and and remainder aremainder a00, that is,, that is,

n = bqn = bq00 + a + a00, where 0 , where 0 a a00 < b. < b.

The remainder aThe remainder a00 is the rightmost digit in the is the rightmost digit in the base b expansion of n.base b expansion of n.

Next, divide qNext, divide q00 by b to obtain: by b to obtain:

qq00 = bq = bq11 + a + a11, where 0 , where 0 a a11 < b. < b.

aa11 is the second digit from the right in the base b is the second digit from the right in the base b expansion of n. Continue this process until you expansion of n. Continue this process until you obtain a quotient equal to zero.obtain a quotient equal to zero.

Page 28: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 28

Representations of IntegersRepresentations of Integers

Example:Example: What is the base 8 expansion of (12345)What is the base 8 expansion of (12345)10 10 ??

First, divide 12345 by 8:First, divide 12345 by 8:12345 = 812345 = 81543 + 11543 + 1

1543 = 81543 = 8192 + 7192 + 7192 = 8192 = 824 + 024 + 024 = 824 = 83 + 03 + 03 = 83 = 80 + 30 + 3

The result is: (12345)The result is: (12345)1010 = (30071) = (30071)88..

Page 29: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 29

Representations of IntegersRepresentations of Integers

procedure procedure base_b_expansion(n, b: positive base_b_expansion(n, b: positive integers)integers)q := nq := nk := 0k := 0whilewhile q q 0 0beginbegin

aakk := q mod b := q mod bq := q := q/bq/bk := k + 1k := k + 1

endend {the base b expansion of n is (a{the base b expansion of n is (ak-1k-1 … a … a11aa00))b b }}

Page 30: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 30

Addition of IntegersAddition of Integers

Let a = (aLet a = (an-1n-1aan-2n-2…a…a11aa00))22, b = (b, b = (bn-1n-1bbn-2n-2…b…b11bb00))2.2.

How can we add these two binary numbers?How can we add these two binary numbers?First, add their rightmost bits:First, add their rightmost bits:

aa00 + b + b00 = c = c002 + s2 + s00,,

where swhere s00 is the is the rightmost bitrightmost bit in the binary in the binary expansion of a + b, and cexpansion of a + b, and c00 is the is the carrycarry..

Then, add the next pair of bits and the carry:Then, add the next pair of bits and the carry:

aa11 + b + b1 1 + c+ c00 = c = c112 + s2 + s11,,

where swhere s11 is the is the next bitnext bit in the binary in the binary expansion of a + b, and cexpansion of a + b, and c11 is the carry. is the carry.

Page 31: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 31

Addition of IntegersAddition of Integers

Continue this process until you obtain cContinue this process until you obtain cn-1n-1..

The leading bit of the sum is sThe leading bit of the sum is snn = c = cn-1n-1..

The result is:The result is:

a + b = (sa + b = (snnssn-1n-1…s…s11ss00))22

Page 32: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 32

Addition of IntegersAddition of Integers

Example:Example:Add a = (1110)Add a = (1110)22 and b = (1011) and b = (1011)22..

aa00 + b + b00 = 0 + 1 = 0 = 0 + 1 = 02 + 1, so that c2 + 1, so that c00 = 0 and s = 0 and s00 = 1. = 1.

aa11 + b + b1 1 + c+ c00 = 1 + 1 + 0 = 1 = 1 + 1 + 0 = 12 + 0, so c2 + 0, so c11 = 1 and s = 1 and s11 = 0. = 0.

aa22 + b + b2 2 + c+ c11 = 1 + 0 + 1 = 1 = 1 + 0 + 1 = 12 + 0, so c2 + 0, so c22 = 1 and s = 1 and s22 = 0. = 0.

aa33 + b + b3 3 + c+ c22 = 1 + 1 + 1 = 1 = 1 + 1 + 1 = 12 + 1, so c2 + 1, so c33 = 1 and s = 1 and s33 = 1. = 1.

ss44 = c = c33 = 1. = 1.

Therefore, s = a + b = (11001)Therefore, s = a + b = (11001)22..

Page 33: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 33

Addition of IntegersAddition of Integers

How do we (humans) add two integers?How do we (humans) add two integers?

Example: Example: 75837583 + + 49324932

5511552211

111111 carrycarry

Binary expansions: Binary expansions: (1011)(1011)22

+ + (1010)(1010)22

1100

carrycarry11

1100

11

11(( ))22

Page 34: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 34

Addition of IntegersAddition of Integers

Let a = (aLet a = (an-1n-1aan-2n-2…a…a11aa00))22, b = (b, b = (bn-1n-1bbn-2n-2…b…b11bb00))2.2.

How can we How can we algorithmically algorithmically add these two add these two binary numbers?binary numbers?First, add their rightmost bits:First, add their rightmost bits:

aa00 + b + b00 = c = c002 + s2 + s00,,

where swhere s00 is the is the rightmost bitrightmost bit in the binary in the binary expansion of a + b, and cexpansion of a + b, and c00 is the is the carrycarry..

Then, add the next pair of bits and the carry:Then, add the next pair of bits and the carry:

aa11 + b + b1 1 + c+ c00 = c = c112 + s2 + s11,,

where swhere s11 is the is the next bitnext bit in the binary expansion in the binary expansion of a + b, and cof a + b, and c11 is the carry. is the carry.

Page 35: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 35

Addition of IntegersAddition of Integers

Continue this process until you obtain cContinue this process until you obtain cn-1n-1..

The leading bit of the sum is sThe leading bit of the sum is snn = c = cn-1n-1..

The result is:The result is:

a + b = (sa + b = (snnssn-1n-1…s…s11ss00))22

Page 36: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 36

Addition of IntegersAddition of Integers

Example:Example:Add a = (1110)Add a = (1110)22 and b = (1011) and b = (1011)22..

aa00 + b + b00 = 0 + 1 = 0 = 0 + 1 = 02 + 1, so that c2 + 1, so that c00 = 0 and s = 0 and s00 = 1. = 1.

aa11 + b + b1 1 + c+ c00 = 1 + 1 + 0 = 1 = 1 + 1 + 0 = 12 + 0, so c2 + 0, so c11 = 1 and s = 1 and s11 = 0. = 0.

aa22 + b + b2 2 + c+ c11 = 1 + 0 + 1 = 1 = 1 + 0 + 1 = 12 + 0, so c2 + 0, so c22 = 1 and s = 1 and s22 = 0. = 0.

aa33 + b + b3 3 + c+ c22 = 1 + 1 + 1 = 1 = 1 + 1 + 1 = 12 + 1, so c2 + 1, so c33 = 1 and s = 1 and s33 = 1. = 1.

ss44 = c = c33 = 1. = 1.

Therefore, s = a + b = (11001)Therefore, s = a + b = (11001)22..

Page 37: Fall 2002CMSC 203 - Discrete Structures1 Let us get into… Number Theory

Fall 2002 CMSC 203 - Discrete Structures 37

Addition of IntegersAddition of Integers

procedure procedure add(a, b: positive integers)add(a, b: positive integers)c := 0c := 0for j := 0 to n-1for j := 0 to n-1beginbegin

d := d := (a(ajj + b + bjj + c)/2 + c)/2ssjj := a := ajj + b + bjj + c – 2d + c – 2dc := dc := d

endendssnn := c := c{the binary expansion of the sum is (s{the binary expansion of the sum is (snnssn-1n-1……ss11ss00))22}}