1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):

Slides:



Advertisements
Similar presentations
DATA REPRESENTATION CONVERSION.
Advertisements

Number Theory and Cryptography
Level ISA3: Information Representation
CSE115/ENGR160 Discrete Mathematics 03/17/11 Ming-Hsuan Yang UC Merced 1.
Chapter Chapter Goals Know the different types of numbers Describe positional notation.
Data Representation ICS 233
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Number System Conversions Lecture L2.2 Section 2.3.
The Binary Number System
Number Systems and Arithmetic
 Binary Binary  Binary Number System Binary Number System  Binary to Decimal Binary to Decimal  Decimal to Binary Decimal to Binary  Octal and Hexadecimal.
Binary Numbers.
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
CSCI 1900 Discrete Structures
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the.
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
Copyright © Cengage Learning. All rights reserved. CHAPTER 2 THE LOGIC OF COMPOUND STATEMENTS THE LOGIC OF COMPOUND STATEMENTS.
Data Representation – Binary Numbers
Computers Organization & Assembly Language
Numbering Systems CS208.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
EX_01.1/46 Numeric Systems. EX_01.2/46 Overview Numeric systems – general, Binary numbers, Octal numbers, Hexadecimal system, Data units, ASCII code,
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
CompSci 102 Discrete Math for Computer Science February 16, 2012 Prof. Rodger.
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Number systems, Operations, and Codes
Operations on Bits Arithmetic Operations Logic Operations
MAT 320 Spring 2008 Section 1.2.  Start with two integers for which you want to find the GCD. Apply the division algorithm, dividing the smaller number.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
Chapter Integers and Algorithms Representations of integers
Positional Number Systems Decimal, Binary, Octal and Hexadecimal Numbers Wakerly Section
AEEE2031 Data Representation and Numbering Systems.
Foundations of Discrete Mathematics Chapter 4 By Dr. Dalia M. Gil, Ph.D.
이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Greatest Common Divisors & Least Common Multiples  Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is.
AF2. Turn off your phones Primes, gcd, some examples, reading.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
ECE DIGITAL LOGIC LECTURE 2: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/14/2016.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Ch04-Number Theory and Cryptography 1. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic.
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
Binary Values. Numbers Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645, 32 Negative Numbers.
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
Week 1(Number System) Muhammad Ammad uddin Logic Design Lab I (CEN211)
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Number Theory. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic principles of divisibility,
The Euclidean Algorithm
Positional Number Systems Decimal, Binary, Octal and Hexadecimal Numbers Wakerly Section
Digital Design Chapter One Digital Systems and Binary Numbers
Number Systems and Binary Arithmetic
COMPUTING FUNDAMENTALS
CSE 102 Introduction to Computer Engineering
CSE 504 Discrete Mathematics & Foundations of Computer Science
Number Theory and Cryptography
IT 0213: INTRODUCTION TO COMPUTER ARCHITECTURE
Applied Discrete Mathematics Week 4: Number Theory
King Fahd University of Petroleum and Minerals
Foundations of Discrete Mathematics
Digital Logic Design (ECEg3141) 2. Number systems, operations & codes 1.
Digital Electronics and Microprocessors
Divisibility and Modular Arithmetic
Presentation transcript:

1 Section 2.5 Integers and Algorithms

2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S): –divide L by S to obtain: L = S * c + r where c is some constant and r is the remainder –next, find gcd(S,r) using the same method –continue as long as r > 0; the last r > 0 is the gcd

3 Example Find gcd(45, 12) gcd(45, 12) = gcd (12, 9) because 45 = 12 * gcd(12, 9) = gcd (9, 3) because 12 = 9 * gcd(9, 3) = 3 because 9 = 3 * Therefore gcd(45, 12) = 3

4 Example Find gcd(271, 83) gcd(271, 83) = gcd(83, 22) because 271 = 83 * gcd(83, 22) = gcd(22, 17) because 83 = 22 * gcd(22, 17) = gcd(17, 5) because 22 = 17 * gcd(17, 5) = gcd(5, 2) because 17 = 5 * gcd(5, 2) = 1 because 5 = 2 * Since 1 is the last non-zero remainder, gcd(271, 83) = 1 and 271 and 83 are relatively prime

5 More on Euclidean Algorithm Euclidean algorithm is based on this lemma: –Let a = bq + r where a, b, q and r are integers –Then gcd(a,b) = gcd(b,r) C++ code for Euclidean Algorithm: int gcd(int a, int b) { int remainder; while (b != 0) { remainder = a % b; a = b; b = remainder; } return a; }

6 Representation of Integers We are used to expressing integers as base 10, or decimal numbers: –Each digit represents a number multiplied by power of 10, with the rightmost digit being multiplied by 10 0 –The sum of the digits represents the value –For example, 472 = 4(10 2 )+7(10 1 )+2(10 0 )

7 Representation of Integers Any base can be used, with the same method employed Computers typically use base 2 (binary), base 8 (octal) and base 16 (hexadecimal) to represent integers

8 Binary Representation Digits are 0s and 1s The value of a binary expansion of a number is the sum of all digits multiplied by the power of 2 represented by their position, with the rightmost digit being position 0 and the leftmost nth position being position n-1

9 Binary Example = 1* * * * * * *2 6 = = 95

10 Hexadecimal Representation Digits range and A.. F –A = 10, B = 11, … F = 15 Hex to decimal conversion example: 14A0E = 14* * * * *16 4 = = 84,494

11 Conversion from hex to binary, and vice-versa Each hex digit represents 4 binary digits, or bits, since 16 = 2 4 Table below shows conversion: HexBinHexBinHexBinHexBin D A1010E B1011F C1100

12 Algorithm for base b expansion of integer n Divide n by b, obtaining quotient & remainder: n = bq 0 + a 0 where 0 <= a 0 < b remainder (a 0 ) is rightmost digit in base b expansion Divide q 0 by b, obtaining: q 0 = bq 1 + a 1 (0 <= a 1 < b) a 1 is second digit from right in base b expansion Continue successive divisions until we obtain a q = 0

13 Example Find octal (base 8) expansion of = 8 * (rightmost digit) 593 =8 * = 8 * = 8 * = 8 * 0 + 1(leftmost digit) Result is

14 C++ for base expansion algorithm (for base <= 10) int baseExpand(int n, int b) { int k = 0, digit, expansion = 0; while (n != 0) { digit = n % b; n = n / b; expansion = expansion + digit * pow(10,k); k++; } return expansion; }

15 Binary Addition Suppose a & b are binary numbers; they can be represented as: a = (a n-1 a n-2 … a 1 a 0 ) b = (b n-1 b n-2 … b 1 b 0 ) where n is the number of digits - note that this is string notation, not indicative of multiplication

16 Binary Addition To add a and b, start with rightmost bits: a 0 + b 0 = s 0 + c 0 * 2 where s is the sum and c is the carry (which may be 0) Proceed to next bit, adding previous carry: a 1 + b 1 + c 0 = s 1 + c 1 * 2

17 Binary Addition Continue process until you reach the last bit of either number: a n-1 + b n-1 + c n-2 = s n-1 + c n-1 * 2 If the carry is not 0, it will be the leading bit of the result If the numbers are not the same length, the carry would be added to the next bit of the longer number, and the carry from this would be added to the next bit, etc.

18 Binary Addition Example Let a = 1110, b = 1001; find a + b a 0 = 0, b 0 = 1 a 0 + b 0 = 1 + 2*0 1 a 1 = 1, b 1 = 0, c 0 = 0 a 1 + b 1 + c 0 = 1 + 2*0 1 a 2 = 1, b 2 = 0, c 1 = 0 a 2 + b 2 + c 1 = 1 + 2*0 1 a 3 = 1, b 3 = 1, c 2 = 0 a 3 + b 3 + c 2 = *0 0 a 4 = 0, b 4 = 0, c 3 = 1 a 4 + b 4 + c 3 =

19 Pseudocode for Addition Algorithm Procedure add (input: positive integers a & b) carry = 0 for (counter = 0; counter < n; counter++) temp =  a counter + b counter + carry/2  sum counter = a counter + b counter + carry – 2 * temp carry = temp sum n = carry

20 Multiplication of Binary Integers Suppose we have 2 n-bit integers a and b; by distributive law, we have: n-1 n-1 ab = a *  b j 2 j =  a(b j 2 j ) j=0 j=0 Note: ab j = a if b j =1, ab j = 0 if b j = 0 Multiply by 2 means shift left and add 0 at end of expansion

21 Multiplication Example 1110 * shift 0000shift 0000shift 1110add

22 Pseudocode for multiplication algorithm Procedure multiply(inputs: a and b, binary expansions of integers with n digits) for (counter = 0; counter < n; counter++) if b counter == 1 ppcounter = a shifted counter spaces (partial product counter) else ppcounter = 0 product = 0 for (counter = 0; counter < n; counter++) product = product + ppcounter

23 Section 2.5 Integers and Algorithms -ends-