module #8: basic number theory
DESCRIPTION
Bogazici University Department of Computer Engineering C mpE 220 Discrete Mathematics 08. Basic Number Theory Haluk Bingöl. Module #8: Basic Number Theory. Rosen 5 th ed., §§2.4-2.5 ~30 slides, ~2 lectures. The Integers and Division. §2.4: The Integers and Division. - PowerPoint PPT PresentationTRANSCRIPT
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
1/18
Mod
ule
#8 –
Num
ber T
heor
y
Bogazici UniversityBogazici UniversityDepartment of Computer EngineeringDepartment of Computer Engineering
CCmpE 220 mpE 220 Discrete Discrete MathematicsMathematics08. Basic Number 08. Basic Number Theory Theory Haluk BingölHaluk Bingöl
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
2/18
Mod
ule
#8 –
Num
ber T
heor
y
Module #8:Module #8:Basic Number TheoryBasic Number Theory
Rosen 5Rosen 5thth ed., §§2.4-2.5 ed., §§2.4-2.5~30 slides, ~2 lectures~30 slides, ~2 lectures
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
3/18
Mod
ule
#8 –
Num
ber T
heor
y
The Integers and DivisionThe Integers and Division
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
4/18
Mod
ule
#8 –
Num
ber T
heor
y§2.4: The Integers and §2.4: The Integers and DivisionDivisionOf course, you already know what the Of course, you already know what the
integers are, and what division is…integers are, and what division is…But:But: There are some specific notations, There are some specific notations,
terminology, and theorems associated terminology, and theorems associated with these concepts which you may not with these concepts which you may not know.know.
These form the basics of These form the basics of number theorynumber theory..Vital in many important algorithms today (hash Vital in many important algorithms today (hash
functions, cryptography, digital signatures).functions, cryptography, digital signatures).
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
5/18
Mod
ule
#8 –
Num
ber T
heor
y
DividesDivides, , FactorFactor, , MultipleMultipleDef.Def. Let Let aa,,bb ℤℤ with with aa00..
aa||bb ““aa dividesdivides bb”” :: ( (ccℤℤ:: b=acb=ac))“There is an integer “There is an integer cc such that such that cc times times a a equals equals b.b.””
Def.Def. If If aa divides divides bb, then we say , then we say aa is a is a factorfactor or a or a divisordivisor of of bb, and , and bb is a is a multiplemultiple of of aa..
ExEx.. 3 312 12 TrueTrue, but 3, but 37 7 FalseFalse..Ex.Ex. “ “bb is even” : is even” :≡ 2|≡ 2|bb. Is 0 even? Is −4?. Is 0 even? Is −4?
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
6/18
Mod
ule
#8 –
Num
ber T
heor
yFacts re: the Divides Facts re: the Divides RelationRelationThm.Thm. aa,,bb,,c c ℤℤ, , aa00
1. 1. aa|0|02. 2. ((aa||bb aa||cc) ) aa | ( | (bb + + cc))3. 3. aa||bb aa||bcbc4. 4. ((aa||bb bb||cc) ) aa||cc
Proof of (2): Proof of (2): aa||bb means there is an means there is an ss such that such that bb==asas, and , and aa||cc means that means that there is a there is a tt such that such that cc==atat, so , so bb++cc = = asas++atat = = aa((ss++tt),), so so aa|(|(bb++cc)) also. also.■■
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
7/18
Mod
ule
#8 –
Num
ber T
heor
yMore Detailed Version of More Detailed Version of ProofProofShow Show aa,,bb,,c c ZZ: (: (aa||bb aa||cc) ) aa | ( | (bb + + cc))..Let Let aa, , bb, , cc be any integers such that be any integers such that aa||bb
and and aa||cc, and show that , and show that aa | ( | (bb + + cc))..By defn. of By defn. of ||, we know , we know ss: : b=asb=as, and , and
tt: : c=atc=at. Let . Let ss, , tt, be such integers., be such integers.Then Then bb++c = asc = as + + at at = = aa((ss++tt)), so , so
uu: : bb++cc==auau, namely , namely uu==ss++tt. . Thus Thus aa||((bb++cc))..
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
8/18
Mod
ule
#8 –
Num
ber T
heor
y
Prime NumbersPrime NumbersDef.Def. An integer An integer pp>1>1 is is primeprime iff iff
it is not the product of two integers it is not the product of two integers greater than 1:greater than 1:pp>1 >1 aa,,bbℕℕ:: aa>1, >1, bb>1, >1, abab==pp..
Remark.Remark. The only positive factors of a prime The only positive factors of a prime pp are 1 and are 1 and pp itself. itself.
Some primes: 2,3,5,7,11,13...Some primes: 2,3,5,7,11,13...Def.Def. Non-prime integers greater than 1 are Non-prime integers greater than 1 are
called called compositecomposite, because they can be , because they can be composedcomposed by multiplying two integers by multiplying two integers greater than 1.greater than 1.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
9/18
Mod
ule
#8 –
Num
ber T
heor
y
Review of §2.4 So FarReview of §2.4 So Faraa||bb “ “aa dividesdivides bb” ” ccZZ:: b=acb=ac““pp is is primeprime” ”
pp>1 >1 aaN:N: (1 < (1 < a a < < p p a|pa|p))Terms Terms factorfactor, , divisordivisor, , multiplemultiple,,
compositecomposite..
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
10/18
Mod
ule
#8 –
Num
ber T
heor
yFundamental Theorem of Fundamental Theorem of ArithmeticArithmeticThm.Thm. Every positive integer has a Every positive integer has a uniqueunique
representation as the product of a non-representation as the product of a non-decreasing series of zero or more decreasing series of zero or more primes.primes.Some examples:Some examples:
1 = (product of empty series) = 11 = (product of empty series) = 12 = 2 (product of series with one element 2 = 2 (product of series with one element
2)2)4 = 2·2 (product of series 2,2)4 = 2·2 (product of series 2,2)2000 = 2·2·2·2·5·5·5; 2001 = 3·23·29;2000 = 2·2·2·2·5·5·5; 2001 = 3·23·29;
2002 = 2·7·11·13; 2003 = 2003 (no clear 2002 = 2·7·11·13; 2003 = 2003 (no clear pattern!)pattern!)Later, we will see how to rigorously prove the
Fundamental Theorem of Arithmetic, starting from scratch!
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
11/18
Mod
ule
#8 –
Num
ber T
heor
yAn Application of Primes!An Application of Primes!When you visit a secure web site (When you visit a secure web site (https:https:… …
address, indicated by padlock icon in IE, key address, indicated by padlock icon in IE, key icon in Netscape), the browser and web site icon in Netscape), the browser and web site may be using a technology called may be using a technology called RSA RSA encryptionencryption..
This This public-key cryptography public-key cryptography scheme involves scheme involves exchanging exchanging public keyspublic keys containing the product containing the product pqpq of two random large primes of two random large primes pp and and qq (a (a private keyprivate key) which must be kept secret by a ) which must be kept secret by a given party.given party.
So, the security of your day-to-day web So, the security of your day-to-day web transactions depends critically on the fact that transactions depends critically on the fact that all known factoring algorithms are intractable!all known factoring algorithms are intractable!Note:Note: There There isis a tractable a tractable quantumquantum algorithm for factoring; so if algorithm for factoring; so if
we can ever build big quantum computers, then RSA is not we can ever build big quantum computers, then RSA is not secure.secure.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
12/18
Mod
ule
#8 –
Num
ber T
heor
y
The Division “Algorithm”The Division “Algorithm”It’s really just a It’s really just a theoremtheorem, not an algorithm… , not an algorithm…
Only called an “algorithm” for historical reasons.Only called an “algorithm” for historical reasons.Thm.Thm. For any integer For any integer dividenddividend aa and and divisordivisor
dd≠0≠0, there is a unique integer , there is a unique integer quotientquotient qq andand remainder remainder rrℕℕ such that such that a a = = dq dq + + rr and and 0 0 r r < |< |d|d|. . Formally, Formally, aa,,ddℤℤ, , dd≠≠0:0: !!qq,,rrℤℤ: 0: 0rr<|<|d|d|, , a=dqa=dq++rr..
We can find We can find qq and and rr by: by: qq==aadd, , rr==aaqdqd..
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
13/18
Mod
ule
#8 –
Num
ber T
heor
y
Greatest Common DivisorGreatest Common DivisorDef.Def. The The greatest common divisorgreatest common divisor gcd(gcd(aa,,bb)) of of
integers integers aa,,bb (not both 0) is the largest (most (not both 0) is the largest (most positive) integer positive) integer dd that is a divisor both of that is a divisor both of aa and of and of bb..dd = gcd( = gcd(aa,,bb) = ) = max(max(dd: : dd||a a dd||bb) )
dd||a a dd||bb eeZZ,, ((ee||aa ee||bb) → ) → d ≥ ed ≥ eExEx.. gcd(24,36)=? gcd(24,36)=?
Positive common divisors: 1,2,3,4,6,12.Positive common divisors: 1,2,3,4,6,12.The largest one of these is 12.The largest one of these is 12.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
14/18
Mod
ule
#8 –
Num
ber T
heor
y
GCD shortcutGCD shortcutIf the prime factorizations are written asIf the prime factorizations are written as and and ,,then the GCD is given by:then the GCD is given by:
Example of using the shortcut:Example of using the shortcut:aa=84=2·2·3·7 = 2=84=2·2·3·7 = 222·3·311·7·711
bb=96=2·2·2·2·2·3 = 2=96=2·2·2·2·2·3 = 255·3·311·7·700
gcd(84,96) = 2gcd(84,96) = 222·3·311·7·70 0 = 2·2·3 = 12.= 2·2·3 = 12.
nan
aa pppa 2121 nb
nbb pppb 2121
.),gcd( ),min(),min(2
),min(1
2211 nn ban
baba pppba
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
15/18
Mod
ule
#8 –
Num
ber T
heor
y
Relative PrimalityRelative PrimalityDef.Def. Integers Integers aa and and bb are called are called relatively relatively
primeprime or or coprimecoprime iff their gcd = 1. iff their gcd = 1.
ExEx.. Neither 21 nor 10 is prime, but they are Neither 21 nor 10 is prime, but they are coprimecoprime. . 21=3·721=3·7 and and 10=2·510=2·5, so they have , so they have no common factors > 1, so their gcd = 1.no common factors > 1, so their gcd = 1.
Def.Def. A A setset of integers { of integers {aa11,,aa22,…} is ,…} is (pairwise) (pairwise) relatively primerelatively prime if all pairs ( if all pairs (aaii, , aajj), for ), for iijj, , are relatively prime.are relatively prime.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
16/18
Mod
ule
#8 –
Num
ber T
heor
y
Least Common MultipleLeast Common MultipleDef. Def. lcm(lcm(aa,,bb)) of positive integers of positive integers aa, , bb, is the , is the
smallest positive integer that is a multiple both smallest positive integer that is a multiple both of of aa and of and of bb. .
mm = lcm( = lcm(aa,,bb) = min() = min(mm: : aa||mm bb||mm)) aa||mm bb||mm nnℤℤ: (: (aa||nn bb||nn) → () → (m ≤ nm ≤ n))
Ex.Ex. lcm(6,10)=30 lcm(6,10)=30Remark.Remark. If the prime factorizations are written If the prime factorizations are written
asas and , and , then the LCM is given bythen the LCM is given by
nan
aa pppa 2121 nb
nbb pppb 2121
.),(lcm ),max(),max(2
),max(1
2211 nn ban
baba pppba
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
17/18
Mod
ule
#8 –
Num
ber T
heor
y
The The modmod operator operatorAn integer “division remainder” operator.An integer “division remainder” operator.Def.Def. Let Let aa,,ddℤℤ with with d>d>11. Then . Then aa modmod dd
denotes the remainder denotes the remainder rr from the division from the division “algorithm” with dividend “algorithm” with dividend aa and divisor and divisor dd; ; i.e.i.e. the remainder when the remainder when aa is divided by is divided by dd..
We can compute We can compute ((aa modmod dd)) by: by: a a dd··aa//dd..In C/C++/Java languages, “In C/C++/Java languages, “%%” = mod.” = mod.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
18/18
Mod
ule
#8 –
Num
ber T
heor
y
Modular CongruenceModular CongruenceDef.Def. ℤℤ ++={={nn ℤℤ | | nn>0}=>0}=ℕℕ−{0}−{0}
(the + integers).(the + integers).Def.Def. Let Let aa,,bbℤℤ, , mm ℤℤ ++..
Then Then aa is congruent tois congruent to b modulo mb modulo m, , written “written “aabb (mod (mod mm)”,)”, iff iff m m | | aabb ..Note: this is a different use of “Note: this is a different use of “” than the ” than the
meaning “is defined as” I’ve used before.meaning “is defined as” I’ve used before.It’s also equivalent to: It’s also equivalent to: ((aabb) mod ) mod m m = 0= 0..
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
19/18
Mod
ule
#8 –
Num
ber T
heor
y
Spiral Visualization of Spiral Visualization of modmod
≡ 3(mod 5)
≡ 2(mod 5)
≡ 1(mod 5)
≡ 0(mod 5)
≡ 4(mod 5) 0
123
4
5
6
78
9
10
11
1213
14
15
16
1718
19
20
21
22
Example shown:modulo-5arithmetic
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
20/18
Mod
ule
#8 –
Num
ber T
heor
yUseful Congruence Useful Congruence TheoremsTheoremsThm.Thm. Let Let aa,,bbℤℤ, , mmℤℤ ++. Then. Then
aabb (mod (mod mm) ) kkℤℤ aa==bb++kmkm..
Thm.Thm. Let Let aa,,bb,,cc,,ddℤℤ, , mmℤℤ ++. Then if . Then if aabb (mod (mod mm)) and and ccdd (mod (mod mm)), then, then▪ ▪ a+ca+c b+db+d (mod (mod mm)), and, and▪▪ ac ac bdbd (mod (mod mm))
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
21/18
Mod
ule
#8 –
Num
ber T
heor
yRosen Rosen §2.5: Integers & §2.5: Integers & AlgorithmsAlgorithmsTopics:Topics:
Euclidean algorithm for finding GCD’s.Euclidean algorithm for finding GCD’s.Base-Base-bb representations of integers. representations of integers.
Especially: binary, hexadecimal, octal.Especially: binary, hexadecimal, octal.Also: Two’s complement Also: Two’s complement
representation of negative numbers.representation of negative numbers.Algorithms for computer arithmetic:Algorithms for computer arithmetic:
Binary addition, multiplication, Binary addition, multiplication, division.division.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
22/18
Mod
ule
#8 –
Num
ber T
heor
y
Euclid’s Algorithm for GCDEuclid’s Algorithm for GCD
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
23/18
Mod
ule
#8 –
Num
ber T
heor
y
Euclid’s Algorithm for GCDEuclid’s Algorithm for GCDFinding GCDs by comparing prime Finding GCDs by comparing prime
factorizations can be difficult when factorizations can be difficult when the prime factors are not known!the prime factors are not known!
Euclid discovered:Euclid discovered: For all For all aa,,bbℤℤ gcd(gcd(aa, , bb) = gcd(() = gcd((aa mod mod bb), ), bb))..
Sort Sort aa,,bb so that so that aa>>bb, and then (given , and then (given bb>1>1)) ((aa mod mod bb) < ) < aa,, so problem is so problem is simplified.simplified.
Euclid of Alexandria
325-265 B.C.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
24/18
Mod
ule
#8 –
Num
ber T
heor
y
Euclid’s Algorithm ExampleEuclid’s Algorithm Examplegcd(372,164) = gcd(372 mod 164, 164).gcd(372,164) = gcd(372 mod 164, 164).
372 mod 164 = 372372 mod 164 = 372164164372/164372/164 = 372 = 372164·2 = 164·2 = 372372328 = 44.328 = 44.
gcd(164,44) = gcd(164 mod 44, 44).gcd(164,44) = gcd(164 mod 44, 44).164 mod 44 = 164164 mod 44 = 1644444164/44164/44 = 164 = 16444·3 = 16444·3 = 164132 = 132 =
32.32.
gcd(44,32) = gcd(44 mod 32, 32) = gcd(12, 32) = gcd(44,32) = gcd(44 mod 32, 32) = gcd(12, 32) = gcd(32 mod 12, 12) = gcd(8,12) = gcd(12 mod 8, gcd(32 mod 12, 12) = gcd(8,12) = gcd(12 mod 8, 8) = gcd(4,8) = gcd(8 mod 4, 4) = gcd(0,4) = 4.8) = gcd(4,8) = gcd(8 mod 4, 4) = gcd(0,4) = 4.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
25/18
Mod
ule
#8 –
Num
ber T
heor
yEuclid’s Algorithm Euclid’s Algorithm PseudocodePseudocodeprocedure procedure gcdgcd((aa, , bb: positive integers): positive integers)
whilewhile b b 0 0 beginbeginrr ≔≔ aa modmod bb; ; aa ≔≔ bb;; b b ≔≔ rr; ; endend
return return aa
Fast! Number of while loop iterationsturns out to be O(log(max(a,b))).
Sorting inputs not needed b/c order will be reversed each iteration.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
26/18
Mod
ule
#8 –
Num
ber T
heor
y
Base-Base-bb NNumber umber SSystemsystems
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
27/18
Mod
ule
#8 –
Num
ber T
heor
y
Base-Base-bb number systems number systemsOrdinarily, we write Ordinarily, we write base-10base-10 representations representations
of numbers, using digits 0-9.of numbers, using digits 0-9.But, 10 isn’t special! Any base But, 10 isn’t special! Any base bb>1 will work.>1 will work.For any positive integers For any positive integers nn,,bb, there is a , there is a
unique sequence unique sequence aak k aakk-1-1… … aa11aa00 of of digitsdigits aaii<b<b such that:such that:
k
i
iiban
0
The “base b expansion
of n”
See module #12 for summation notation.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
28/18
Mod
ule
#8 –
Num
ber T
heor
y
Particular Bases of InterestParticular Bases of Interest
Base Base bb=10 (decimal):=10 (decimal):10 digits: 10 digits: 0,1,2,3,4,5,6,7,8,90,1,2,3,4,5,6,7,8,9..
Base Base bb=2 (binary):=2 (binary):2 digits: 2 digits: 0,10,1. (“Bits”=“. (“Bits”=“bibinary dignary digitsits.”).”)
Base Base bb=8 (octal):=8 (octal):8 digits: 8 digits: 0,1,2,3,4,5,6,70,1,2,3,4,5,6,7..
Base Base bb=16 (hexadecimal):=16 (hexadecimal):16 digits: 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Usedinternally in all modern computers
Octal digits correspond to groups of 3 bits
Hex digits give groups of 4 bits
Used only because we have 10 fingers
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
29/18
Mod
ule
#8 –
Num
ber T
heor
y
Converting to Base Converting to Base bb(An algorithm, informally stated.)(An algorithm, informally stated.)To convert any integer To convert any integer nn to any base to any base bb>1:>1:To find the value of the To find the value of the rightmostrightmost (lowest- (lowest-
order) digit, simply compute order) digit, simply compute nn mod mod bb..Now, replace Now, replace nn with the quotient with the quotient nn//bb..Repeat above two steps to find Repeat above two steps to find
subsequent digits, until subsequent digits, until nn is gone (=0). is gone (=0).
Exercise for student: Write this out in pseudocode…
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
30/18
Mod
ule
#8 –
Num
ber T
heor
y
Addition of Binary NumbersAddition of Binary Numbers
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
31/18
Mod
ule
#8 –
Num
ber T
heor
y
Addition of Binary NumbersAddition of Binary Numbersprocedureprocedure addadd((aann−1−1……aa00, , bbnn−1−1……bb00: binary representations of : binary representations of
non-negative integers non-negative integers aa,,bb))carry carry := 0:= 0forfor bitIndexbitIndex := 0 := 0 to to nn−1−1 {go through bits}{go through bits}
bitSum bitSum :=:= a abitIndexbitIndex++bbbitIndexbitIndex+carry +carry {2-bit sum}{2-bit sum}ssbitIndexbitIndex := := bitSumbitSum modmod 2 2 {low bit of sum}{low bit of sum}carrycarry := := bitSum bitSum / 2/ 2 {high bit of sum}{high bit of sum}
ssnn := := carrycarryreturnreturn ssnn……ss00: binary representation of integer : binary representation of integer ss
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
32/18
Mod
ule
#8 –
Num
ber T
heor
y
Two’s ComplementTwo’s ComplementIn binary, negative numbers can be conveniently In binary, negative numbers can be conveniently
represented using represented using two’s complement notationtwo’s complement notation..In this scheme, a string of In this scheme, a string of n n bits can represent any bits can represent any
integer integer ii such that such that −2−2nn−1−1 ≤ ≤ ii < 2 < 2nn−1−1..The bit in the highest-order bit-position (#The bit in the highest-order bit-position (#nn−1−1) )
represents a coefficient multiplying represents a coefficient multiplying −2−2nn−1−1;;The other positions The other positions ii < < nn−1−1 just represent just represent 22ii, as before., as before.
The The negationnegation of any of any nn-bit two’s complement -bit two’s complement number number aa = = aann−1−1……aa00 is given is given by by aann−1−1……aa00 + 1 + 1..
The bitwise logical complement of the n-bit string an−1…a0.
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
33/18
Mod
ule
#8 –
Num
ber T
heor
yCorrectness of Negation Correctness of Negation AlgorithmAlgorithmThm.Thm. For an integer For an integer aa represented in two’s represented in two’s
complement notation, complement notation, −−aa = = aa + 1 + 1..
ProofProof.. aa = − = −aann−1−122nn−1−1 + + aann−2−222nn−2−2 + … + + … + aa002200, , so so −−aa = = aann−1−122nn−1−1 − − aann−2−222nn−2−2 − … − − … − aa002200. . Note Note aann−1−122nn−1−1= (1−= (1−aann−1−1)2)2nn−1 −1 = 2= 2nn−1 −1 − − aann−1−122nn−1−1. . But But 22nn−1−1 = 2 = 2nn−2−2 + … + 2 + … + 200 + 1 + 1. . So we have So we have −−aa = − = − aann−1−122nn−1−1 + (1− + (1−aann−2−2)2)2nn−2−2 + … + (1−+ … + (1−aa00)2)200 + 1 = + 1 = aa + 1 + 1..
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
34/18
Mod
ule
#8 –
Num
ber T
heor
ySubtraction of Binary Subtraction of Binary NumbersNumbersprocedureprocedure subtractsubtract((aann−1−1……aa00, , bbnn−1−1……bb00: binary two’s : binary two’s
complement reps. of integers complement reps. of integers aa,,bb))returnreturn addadd((aa, , addadd((bb,1)) ,1)) { { aa + (− + (−bb) }) }
Note that this fails if either of the Note that this fails if either of the adds causes a carry into or out of adds causes a carry into or out of the the nn−1 position, since −1 position, since 22nn−2−2+2+2nn−2−2 ≠ ≠ −2−2nn−1−1, and , and −2−2nn−1 −1 + (−2+ (−2nn−1−1) = −2) = −2nn isn’t representable! We call this an isn’t representable! We call this an overflowoverflow..
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
35/18
Mod
ule
#8 –
Num
ber T
heor
yMultiplication of Binary Multiplication of Binary NumbersNumbersprocedureprocedure multiplymultiply((aann−1−1……aa00, , bbn−n−11……bb00: binary : binary
representations of representations of aa,,bbNN))productproduct := 0 := 0forfor ii := 0 to := 0 to nn−1−1
ifif bbii = 1 = 1 thenthen productproduct := := addadd((aann−1−1……aa0000ii, , productproduct))
returnreturn productproduct
i extra 0-bitsappended afterthe digits of a
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
36/18
Mod
ule
#8 –
Num
ber T
heor
yBinary Division with Binary Division with RemainderRemainderprocedureprocedure div-moddiv-mod((aa,,dd ZZ++) ) {Quotient & rem. of {Quotient & rem. of
aa//dd.}.}nn := max(length of := max(length of aa in bits, length of in bits, length of dd in bits) in bits)forfor ii := := nn−1−1 downto downto 00
ifif a ≥ da ≥ d00ii then then {Can we subtract at {Can we subtract at this position?}this position?}
qqii :=:= 11 {This bit of {This bit of quotient is 1.}quotient is 1.}
aa := := aa − − dd00ii {Subtract to get {Subtract to get remainder.}remainder.}
elseelseqqii := 0:= 0 {This bit of {This bit of
quotient is 0.}quotient is 0.}rr := := aareturnreturn qq,,rr {{qq = quotient, = quotient, rr = remainder} = remainder}
Base
d on
Ros
en, D
iscre
te M
athe
mat
ics &
Its
Appl
icatio
ns, 5
ePr
epar
ed b
y (c
)200
1-20
04 M
ichae
l P. F
rank
M
odifi
ed b
y (c
) 200
4-20
05 H
aluk
Bin
göl
37/18
Mod
ule
#8 –
Num
ber T
heor
y
ReferencesReferences• RosenRosen
Discrete Mathematics and its Discrete Mathematics and its Applications, 5eApplications, 5eMc GrawHill, 2003Mc GrawHill, 2003