Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
CS1010 Programming Methodology
Advertisements

CSE 311 Foundations of Computing I Lecture 13 Number Theory Autumn 2012 CSE
ACM Workshop Number Theory.
Cryptography and Network Security Chapter 4
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
Chapter 4 – Finite Fields Introduction  will now introduce finite fields  of increasing importance in cryptography AES, Elliptic Curve, IDEA, Public.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
CSE 311 Foundations of Computing I Lecture 12 Primes, GCD, Modular Inverse Spring
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
CS555Spring 2012/Topic 61 Cryptography CS 555 Topic 6: Number Theory Basics.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
CSCI 1900 Discrete Structures
9/2/2015Discrete Structures1 Let us get into… Number Theory.
Mathematics of Cryptography Part I: Modular Arithmetic
3.4/3.5 The Integers and Division/ Primes and Greatest Common Divisors Let each of a and b be integers. We say that a divides b, in symbols a | b, provided.
MATH 224 – Discrete Mathematics
Module :MA3036NI Cryptography and Number Theory Lecture Week 7
CPSC 3730 Cryptography and Network Security
1 Cryptography and Network Security Third Edition by William Stallings Lecture slides by Lawrie Brown Chapter 4 – Finite Fields.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Approximate computations Jordi Cortadella Department of Computer Science.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The RSA Algorithm. Content Review of Encryption RSA An RSA example.
Chapter 4 – Finite Fields
Polynomials Jordi Cortadella Department of Computer Science.
YSLInformation Security -- Public-Key Cryptography1 Prime and Relatively Prime Numbers Divisors: We say that b  0 divides a if a = mb for some m, where.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
CS/ECE Advanced Network Security Dr. Attila Altay Yavuz
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
Information Security Lab. Dept. of Computer Engineering 87/121 PART I Symmetric Ciphers CHAPTER 4 Finite Fields 4.1 Groups, Rings, and Fields 4.2 Modular.
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
CS Modular Division and RSA1 RSA Public Key Encryption To do RSA we need fast Modular Exponentiation and Primality generation which we have shown.
MA/CSSE 473 Day 08 Extended Euclid's Algorithm Modular Division Fermat's little theorem.
Cryptography and Network Security Chapter 4. Introduction  will now introduce finite fields  of increasing importance in cryptography AES, Elliptic.
CSE 311 Foundations of Computing I Lecture 14 Euclid’s Algorithm Mathematical Induction Autumn 2012 CSE
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Ref: Pfleeger96, Ch.31 Properties of Arithmetic Reference: Pfleeger, Charles P., Security in Computing, 2nd Edition, Prentice Hall, 1996.
AF2. Turn off your phones Primes, gcd, some examples, reading.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
MA/CSSE 473 Day 07 Euclid's Algorithm. MA/CSSE 473 Day 07 Student Questions Review topics not covered in class Euclid's algorithm (if there is time) extended.
Functions Jordi Cortadella Department of Computer Science.
AF2. Turn off your phones Primes, gcd, some examples, reading.
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.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Number Theory. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic principles of divisibility,
Number-Theoretic Algorithms
CMPS 2433 – Coding Theory Chapter 3
Discrete Math II Howon Kim
Greatest Common Divisor
Greatest Common Divisor
Reasoning with invariants
For loops, nested loops and scopes
CSE 311 Foundations of Computing I
MATH301- DISCRETE MATHEMATICS Copyright © Nahid Sultana Dr. Nahid Sultana Chapter 4: Number Theory and Cryptography.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Number Theory (Chapter 7)
Topic 6: Number Theory Basics
Foundations of Discrete Mathematics
Lecture 20 Guest lecturer: Neal Gupta
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Search algorithms for vectors
Mathematical Background for Cryptography
Useful GCD Fact If a and b are positive integers, then gcd(a,b) = gcd(b, a mod b) Proof: By definition of mod, a = qb+ (a mod b) for.
Presentation transcript:

Greatest Common Divisor Jordi Cortadella Department of Computer Science

Simplifying fractions greatest common divisor Introduction to Programming© Dept. CS, UPC2

The largest square tile What is the largest square tile that can exactly cover a w  h rectangle? w = 60 h=24 44444444 90 tiles of side-length 4 Introduction to Programming© Dept. CS, UPC3

The largest square tile What is the largest square tile that can exactly cover a w  h rectangle? w = 60 h=24 66666666 90 tiles of side-length 4 40 tiles of side-length 6 Introduction to Programming© Dept. CS, UPC4

The largest square tile What is the largest square tile that can exactly cover a w  h rectangle? w = 60 h=24 90 tiles of side-length 4 40 tiles of side-length 6 10 tiles of side-length  12 Introduction to Programming© Dept. CS, UPC5

The largest square tile What is the largest square tile that can exactly cover a w  h rectangle? w = 60 h=24 12   24 = (5  12)  (2  12) = (5  2)  (12  12) gcd(60, 24) largest tile number of tiles Introduction to Programming© Dept. CS, UPC6

Euclid (300 B.C.) A fragment of Euclid’s Elements Introduction to Programming© Dept. CS, UPC7

Euclidean algorithm for gcd Introduction to Programming© Dept. CS, UPC8

Euclidean algorithm for gcd Introduction to Programming© Dept. CS, UPC

Euclidean algorithm for gcd Properties: gcd(a, a) = a; gcd(a, 0) = a If a  b, then gcd(a, b) = gcd(a  b, b) Example: ab gcd (114, 42) Introduction to Programming© Dept. CS, UPC10

Euclidean algorithm for gcd // Pre: a > 0, b > 0 // Returns the greatest common divisor of a and b int gcd(int a, int b) { while (a != b) { if (a > b) a = a – b; else b = b – a; } return a; } Introduction to Programming© Dept. CS, UPC11

Euclidean algorithm for gcd iterationab 010,000, ,999, ,999, ,999, …… 64, , , , , , …11… 64, , Introduction to Programming© Dept. CS, UPC12

Faster Euclidean algorithm for gcd Properties: gcd(a, 0) = a If b  0 then gcd(a, b) = gcd(a%b, b) Example ab 10,000, Introduction to Programming© Dept. CS, UPC13

// Pre: a  0, b  0 // Returns the greatest common divisor of a and b int gcd(int a, int b) { while (a != 0 and b != 0) { if (a > b) a = a%b; else b = b%a; } return a + b; } Faster Euclidean algorithm for gcd Termination: a == 0 or b == 0 not Introduction to Programming© Dept. CS, UPC14

ab Faster Euclidean algorithm for gcd // Pre: a  0, b  0 // Returns the greatest common divisor of a and b int gcd(int a, int b) { while (b > 0) { int r = a%b; a = b; b = r; } return a; } yyxx yyx%yx%y > a b Introduction to Programming© Dept. CS, UPC15

Comparing algorithms for gcd int gcd(int a, int b) { while (b > 0) { int r = a%b; a = b; b = r; } return a; } int gcd(int a, int b) { while (a > 0 and b > 0) { if (a > b) a = a%b; else b = b%a; } return a + b; } Every iteration: 3 comparisons 1 mod operation Every iteration: 1 comparison 1 mod operation Introduction to Programming© Dept. CS, UPC16

Efficiency of the Euclidean algorithm How many iterations will Euclid’s algorithm need to calculate gcd(a,b) in the worst case (assume a > b)? – Subtraction version: a iterations (consider gcd( , 1)) – Modulo version:  5  d(b) iterations, where d(b) is the number of digits of b (Gabriel Lamé, 1844) Introduction to Programming© Dept. CS, UPC17

Binary Euclidean algorithm Computers can perform  2 and /2 operations efficiently (217) 10 = ( ) 2 (2172) 10 = (434) 10 = ( ) 2 (217/2) 10 = (108) 10 = ( ) 2 (217%2) 10 = 1 (least significant bit) Introduction to Programming© Dept. CS, UPC18

Binary Euclidean algorithm Assume a  b: abgcd(a,b) a0a even 2gcd(a/2, b/2) evenoddgcd(a/2, b) oddevengcd(a,b/2) odd gcd((a-b)/2, b) ab 22 22 33 12 Introduction to Programming© Dept. CS, UPC19

// Pre: a  0, b  0 // Returns the greatest common divisor of a and b int gcd(int a, int b) { int r = 1; // Accumulates common powers of two while (a != 0 and b != 0) { if (a%2 == 0 and b%2 == 0) { a = a/2; b = b/2; r = r2; } else if (a%2 == 0) a = a/2; else if (b%2 == 0) b = b/2; else if (a > b) a = (a - b)/2; else b = (b – a)/2; } return (a + b)r; } Binary Euclidean algorithm Introduction to Programming© Dept. CS, UPC20

// Pre: a  0, b  0 // Returns the greatest common divisor of a and b int gcd(int a, int b) { if (a == 0 or b == 0) return a + b; int r = 1; // Accumulates common powers of two while (a%2 == 0 and b%2 == 0) { a = a/2; b = b/2; r = r2; } while (a != b) { if (a%2 == 0) a = a/2; else if (b%2 == 0) b = b/2; else if (a > b) a = (a - b)/2; else b = (b – a)/2; } return ar; } Binary Euclidean algorithm Introduction to Programming© Dept. CS, UPC21

Summary Euclid’s algorithm is very simple. It is widely used in some applications related to cryptography (e.g., electronic commerce). Euclid’s algorithm efficiently computes the gcd of very large numbers. Question: how would you compute the least common multiple of two numbers efficiently? Introduction to Programming© Dept. CS, UPC22