Base-b representations of integers. (b 진법표현 ) Algorithms for computer arithmetic: Euclidean algorithm for finding GCD s.

Similar documents
2.5 정수와알고리즘 (Integers and Algorithms)

COT 3100 Applications of Discrete Structures Dr. Michael P. Frank

Discrete Mathematics GCD, LCM, RSA Algorithm

Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices. Integers & Algorithms (2.5)

Conversions between Decimal and Binary

Mat Week 8. Week 8. gcd() Mat Bases. Integers & Computers. Linear Combos. Week 8. Induction Proofs. Fall 2013

Ch 4.2 Divisibility Properties

Student Responsibilities Week 8. Mat Section 3.6 Integers and Algorithms. Algorithm to Find gcd()

With Question/Answer Animations. Chapter 4

MATH Dr. Halimah Alshehri Dr. Halimah Alshehri

3 The fundamentals: Algorithms, the integers, and matrices

CSE 311: Foundations of Computing. Lecture 12: Two s Complement, Primes, GCD

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

4 Number Theory and Cryptography

14:332:231 DIGITAL LOGIC DESIGN. Why Binary Number System?

Menu. Review of Number Systems EEL3701 EEL3701. Math. Review of number systems >Binary math >Signed number systems

CPSC 467: Cryptography and Computer Security

CSE 311 Lecture 13: Primes and GCD. Emina Torlak and Kevin Zatloukal

Four Important Number Systems

ECE380 Digital Logic. Positional representation

4. Number Theory (Part 2)

The Euclidean Algorithm and Multiplicative Inverses

CPSC 467b: Cryptography and Computer Security

Numbering Systems. Contents: Binary & Decimal. Converting From: B D, D B. Arithmetic operation on Binary.

cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications

Remainders. We learned how to multiply and divide in elementary

EE260: Digital Design, Spring n Digital Computers. n Number Systems. n Representations. n Conversions. n Arithmetic Operations.

Math.3336: Discrete Mathematics. Primes and Greatest Common Divisors

Number Theory: Applications. Number Theory Applications. Hash Functions II. Hash Functions III. Pseudorandom Numbers

Arithmetic Algorithms, Part 1

Proofs. Methods of Proof Divisibility Floor and Ceiling Contradiction & Contrapositive Euclidean Algorithm. Reading (Epp s textbook)

Numbers. Çetin Kaya Koç Winter / 18

The following is an informal description of Euclid s algorithm for finding the greatest common divisor of a pair of numbers:

Intermediate Math Circles February 26, 2014 Diophantine Equations I

COMP239: Mathematics for Computer Science II. Prof. Chadi Assi EV7.635

Chapter 5. Number Theory. 5.1 Base b representations

Section Summary. Division Division Algorithm Modular Arithmetic

Part I, Number Systems. CS131 Mathematics for Computer Scientists II Note 1 INTEGERS

This is a recursive algorithm. The procedure is guaranteed to terminate, since the second argument decreases each time.

Lecture Notes. Advanced Discrete Structures COT S

ENGIN 112 Intro to Electrical and Computer Engineering

Notes. Number Theory: Applications. Notes. Number Theory: Applications. Notes. Hash Functions I

8 Primes and Modular Arithmetic

0,..., r 1 = digits in radix r number system, that is 0 d i r 1 where m i n 1

14:332:231 DIGITAL LOGIC DESIGN. 2 s-complement Representation

CS1800 Discrete Structures Fall 2017 October, CS1800 Discrete Structures Midterm Version A

ENGIN 112 Intro to Electrical and Computer Engineering

Applied Cryptography and Computer Security CSE 664 Spring 2017

Algorithms CMSC Basic algorithms in Number Theory: Euclid s algorithm and multiplicative inverse

Mathematical Induction

CHAPTER 3. Congruences. Congruence: definitions and properties

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

1. (16 points) Circle T if the corresponding statement is True or F if it is False.

CSE 20 DISCRETE MATH. Fall

Lecture 4: Number theory

not to be republished NCERT REAL NUMBERS CHAPTER 1 (A) Main Concepts and Results

Number Theory Basics Z = {..., 2, 1, 0, 1, 2,...} For, b Z, we say that divides b if z = b for some. Notation: b Fact: for all, b, c Z:

CISC-102 Winter 2016 Lecture 11 Greatest Common Divisor

NUMBERS AND CODES CHAPTER Numbers

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

The set of integers will be denoted by Z = {, -3, -2, -1, 0, 1, 2, 3, 4, }

Number Theory and Cryptography

A field F is a set of numbers that includes the two numbers 0 and 1 and satisfies the properties:

CISC 1400 Discrete Structures

Mathematical Foundations of Cryptography

Sets. We discuss an informal (naive) set theory as needed in Computer Science. It was introduced by G. Cantor in the second half of the nineteenth

ECE260: Fundamentals of Computer Engineering

Digital Systems Overview. Unit 1 Numbering Systems. Why Digital Systems? Levels of Design Abstraction. Dissecting Decimal Numbers

The Euclidean Algorithm

Finite Fields. Mike Reiter

MAT 243 Test 2 SOLUTIONS, FORM A

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

a the relation arb is defined if and only if = 2 k, k

Exercises Exercises. 2. Determine whether each of these integers is prime. a) 21. b) 29. c) 71. d) 97. e) 111. f) 143. a) 19. b) 27. c) 93.

Algorithms (II) Yu Yu. Shanghai Jiaotong University

Direct Proof MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Direct Proof Fall / 24

2301 Assignment 1 Due Friday 19th March, 2 pm

Lecture 2. The Euclidean Algorithm and Numbers in Other Bases

4 Powers of an Element; Cyclic Groups

CSEN102 Introduction to Computer Science

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Basic elements of number theory

Basic elements of number theory

Number Theory. CSS322: Security and Cryptography. Sirindhorn International Institute of Technology Thammasat University CSS322. Number Theory.

CS1800 Discrete Structures Final Version A

Introduction to Number Theory. The study of the integers

Induction and recursion. Chapter 5

Some Facts from Number Theory

Binary addition example worked out

CHAPTER 1 REAL NUMBERS KEY POINTS

18 Divisibility. and 0 r < d. Lemma Let n,d Z with d 0. If n = qd+r = q d+r with 0 r,r < d, then q = q and r = r.

ICS141: Discrete Mathematics for Computer Science I

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

hexadecimal-to-decimal conversion

Addition. Ch1 - Algorithms with numbers. Multiplication. al-khwārizmī. al-khwārizmī. Division 53+35=88. Cost? (n number of bits) 13x11=143. Cost?

CSE 20: Discrete Mathematics

CSE 241 Digital Systems Spring 2013

CSCI 255. S i g n e d N u m s / S h i f t i n g / A r i t h m e t i c O p s.

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 3

Transcription:

이산수학 () 정수와알고리즘 (Integers and Algorithms) 2011년봄학기 강원대학교컴퓨터과학전공문양세

Introduction Base-b representations of integers. (b 진법표현 ) Especially: binary, hexadecimal, octal. Also, two s complement representation (2 의보수표현 ) Algorithms for computer arithmetic: Binary addition, multiplication, division. Euclidean algorithm for finding GCD s. Page 2

Base-b Representations of Integers If b is a positive integer greater than 1, then a given positive integer n can be uniquely represented as follows: n = a k-1 k b k + a k-1 b + + a 1 b 1 + a 0 b 0 where k is a natural number. and a 0, a 1,, and a k are a natural number less than b. a k 0. Example: 165 = 1 10 2 + 6 10 1 + 5 10 0 = (165) 10 165 = 2 8 2 + 4 8 1 + 5 8 0 = (245) 8 Page 3

Base-b Number Systems Ordinarily we write base-10 representations of numbers (using digits 0-9). However, 10 isn t special; any base b>1 will work. For any positive integers n, b, there is a unique sequence a k a k-1 a 1 a 0 of digits a i <b such that n k i 0 i a i b The base b expansion of n (n 의밑수b 전개, n의 b진법표현 ) Page 4

Particular Bases of Interest Base b=10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9. Base b=2 2 (binary): 2 digits: 0,1. ( Bits = binary digits. ) Base b=8 (octal): 8 digits: 0,1,2,3,4,5,6,7. Used only because we have 10 fingers Used internally in all modern computers Octal digits correspond to groups of 3 bits Base b=16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Hex digits give groups of 4 bits Page 5

Converting to Base b (1/2) Informal Algorithm 1. To convert any integer n to any base b>1: 2. To find the value of the rightmost (lowest-order) digit, simply compute n mod b. (n%b 로가장끝자리 (digit) 를찾는다.) 3. Now replace n with the quotient n/b. ( 다음자리 (digit) 을구하기위하여, 몫을 n 으로삼는다.) 4. Repeat above two steps to find subsequent digits, until n is gone (=0). ( 단계 2/3 을 n 이 0 이될때까지반복한다.) (177130) 10 = (?) 16 (241) 10 = (?) 2 177130 = 16 11070 + 10 241 = 2 120 + 1, 120 = 2 60 + 0 11070 = 16 691691 + 14 60 = 2 30 + 0, 30 = 2 15 + 0 691 = 16 43 + 3 15 = 2 7 + 1, 7 = 2 3 + 1 43 = 16 2 + 11 3 = 2 1 + 1, 1 = 2 0 + 1 2 = 16 0 + 2 (241) 10 = (11110001) 2 (177130) 10 = (2B3EA) 16 Page 6

Converting to Base b (2/2) Formal Algorithm procedure base b expansion (n: positive integer) q := n k := 0 while q 0 begin a k := q mod b {remainder} q := q/b {quotient} k := k + 1 end {the base b expansion of n is (a k a k-1 a 1 a 0 ) b } Page 7

Addition of Binary Numbers Intuition (let a = (a n-1 a 1 a 0 ) 2, b = (b n-1 b 1 b 0 ) 2 ) c n-1 c n-2... c 1 c 0 c i = (a i-1 +b i-1 +c i-1 )/2 a = a n-1 a n-2... a 2 a 1 a 0 b = b n-1 b n-2... b 2 b 1 b 0 a+b = s n s n-1 s n-2... s 2 s 1 s 0 s i = (a i +b i +c i )%2 Algorithm procedure add(a n 1 a 0, b n 1 b 0 : binary expressions of a,b) c := 0 {c mean a carry} for i := 0 to n 1 {i means a bit index} begin sum := a i + b i + c {2-bit sum} O(n) s i := sum mod 2 {low bit of sum} c := sum/2 {high bit of sum} end s n := c {the binary expression of the sum is (s n s n-1 s 1 s 0 ) 2 } Page 8

2 s Complement (2의보수 ) (1/2) In binary, negative numbers can be conveniently represented using 2 s s complement notation. ( 실제로, 컴퓨터에서는음수를 2 의보수로표현한다.) In this scheme, a string of n bits can represent integers 2 n 1 ~ (2 n 1 1). 0 이상의정수만표현한다면 0 ~ 2 n 1 (unsigned int n) 음의정수까지표현한다면 2 n-1 ~ 2 n-1 1 (int n) The bit in the highest-order h bit-position iti (n 1) represents a coefficient multiplying 2 n 1 ; ( 왼쪽첫번째 bit 는 22 n 1 을나타내며, 흔히부호 (+ or ) ) 를의미한다.) The other positions i < n 1 just represent 2 i, as before. Page 9

2 s Complement (2의보수 ) (2/2) The negation of any n-bit 2 s complement number a(= a n 1 a 0 ) is given by a n 1 a 0 + 1. Bitwise logical complement of a Examples 1011 = (0100 + 1) = (0101) = (5) 10 0100 = +0100 = (4) 10 Page 10

Subtraction of Binary Numbers Theorem: For an integer a represented in 2 s complement notation, a = a + 1. (a 가 2 보수로표현된다면, ) Proof: Just try it by yourself! Algorithm procedure subtract (a n 1 a 0, b n 1 b 0 : binary 2 s complement expressions of a,b) return add(a, add(b, 1)) { a + ( b) } Page 11

Multiplication of Binary Numbers (1/2) Intuition (let a = (a n-1 a 1 a 0 ) 2, b = (b n-1 b 1 b 0 ) 2 ) a = a n-1 a n-2... a 2 a 1 a 0 b = b n-1 b n-2... b 2 b 1 b 0 c 0 = s (n-1,0) s (n-2,0)... s (2,0) s (1,0) s (0,0) c 1 = s (n-1,1) s (n-2,1)... s (2,1) s (1,1) s (0,1) 0 c 2 = s (n-1,2) s (n-2,2)... s (2,2) s (1,2) s (0,2) 0 0 +)...... a b = c n-1 + c n-2 +... + c 2 + c 1 + c 0 s (i,j) = (if b j = 1 then a i else 0) c j = (if b j = 1 then a << j else 0) Page 12

Multiplication of Binary Numbers (2/2) Algorithm procedure multiply(a n 1 a 0, b n 1 b 0 : binary expressions of a,b) for j := 0 to n 1 {a bit index for b} begin if b j = 1 then c j := a shifted j places {c j := a << j} else c j := 0 end {c 1, c 2,, c n-1 are the partial products.} p := 0 for j := 0 to n 1 p := add(p, c j ) {p is the value of ab} O(n 2 ) Can be reduced to O(n 1.585 ) Page 13

Division Algorithm Example: 23/4? r q Algorithm 23 4 = 19 1 19 4 = 15 2 15 4 = 11 3 11 4 = 7 4 7 4 = 3 5 procedure division(a, d: positive integer) q := 0 r := a while r d begin r := r d q := q + 1 end {q is the quotient(= a/d ), r is the remainder(=a%d)} Page 14

Euclid s Algorithm for GCD Finding GCDs by comparing prime factorizations can be difficult if the prime factors are unknown. ( 소인수분해로최대공약수를구하는것은어렵다. 특히, 큰수인경우 ) Euclid discovered: For all integers a, b, gcd(a, b) = gcd((a mod b), b). Sort a,b so that t a>b, and then (given b>1) (a mod b) < a, so problem is simplified. Examples gcd(372, 164) = gcd(372 mod 164, 164) [372%164 = 44] gcd(164, 44) = gcd(164 mod 44, 44) [164%44 = 32] gcd(44, 32) = gcd(44 mod 32, 32) = gcd(12, 32) = gcd(12, 8) gcd(8, 4) = gcd(4, 0) = 4. Page 15

Proof of Euclid s Algorithm Prove gcd(a,b) = gcd(b,r) where r = a bq a 와 b 의공약수는 b 와 r 의공약수가같음을보이면, 양쪽공약수의쌍이같으므로 ( 최대공약수가같아져 ) 증명이이루어진다. d 를 a 와 b 의공약수라하자. 그러면, a = dq a, b = dq b 가성립한다. 정의에의해, r = dq a dq b q= d(q a q b q) ) 이성립하므로, d 는 r 의약수이다. 따라서, d 는 b 와 r 의공약수이다. d 를 b 와 r 의공약수라하자. 그러면, b = dq b, r = dq r 이성립한다. 정의에의해, a = dq r + dq b q= d(q r + q b q) 이성립하므로, d 는 a 의약수이다. 따라서, d 는 a 와 b 의공약수이다. Page 16

Euclid s Algorithms Algorithm in Pseudocode procedure gcd(a, b: positive integer) while b 0 begin r := a mod b {r = a % b} a := b b := r end return a Algorithm in C (using recursive calls) int gcd(int a, int b) /* assume a > b */ { if(b==0) return a; else return gcd(b, a%b); } Page 17