SlideShare a Scribd company logo
2
Most read
6
Most read
8
Most read
EUCLID’S ALGORITHM 
FOR FINDING 
GREATEST COMMON DIVISOR
EUCLID’S ALGORITHM 
(Quick history of this recently discovered algorithm) 
Euclid's Algorithm appears as the solution to the Proposition 
VII.2 in the Elements (written around 300 BC): 
Given two numbers not prime to one another, to find their 
greatest common measure. 
What Euclid called "common measure" is termed nowadays 
a common factor or a common divisor. Euclid VII.2 then 
offers an algorithm for finding the greatest common 
divisor(gcd) of two integers. Not surprisingly, the algorithm 
bears Euclid's name. 
Algorithms - Arora Euclid's Algorithm - GCD 2
EUCLID’S ALGORITHM (CONT. ) 
// Preconditions: n > m > 0 
// Return an error if preconditions are not met. 
// n%m is the remainder after dividing n with m. 
Recursive version 
gcd(n,m) 
r = n%m 
if r == 0 return m 
// else 
return gcd(m, r) 
Iterative version 
gcd(n,m) 
r = n%m 
while (r > 0) { 
n = m 
m = r 
r = n%m 
} 
return m 
Algorithms - Arora Euclid's Algorithm - GCD 3
EUCLID’S ALGORITHM (CONT. ) 
The algorithm is based on the following two observations: 
1. If m divides n, then gcd(n, m) = n. 
2. If n = mk + r, for integers k and r, then gcd(n, m) = gcd(m, 
r). Indeed, every common divisor of n and m also divides 
r. Thus gcd(n, m) divides r. But, of course, gcd(n, m)|m. 
Therefore, gcd(n, m) is a common divisor of m and r and 
hence gcd(n, m) ≤ gcd(m, r). The reverse is also true 
because every divisor of m and r also divides n. 
Algorithms - Arora Euclid's Algorithm - GCD 4
ANALYZING EUCLID’S ALGORITHM 
 What is the relationship between n, m and r? 
 There is no known relationship between n and m, other than the 
precondition that n > m. 
 But we can claim something more interesting between n and r 
 r < m 
 Also, r  n - m 
  r < n/2 
 Af ter 2 recursive cal ls (or two iterations of the loop) , the first 
argument is no more than hal f . 
 This leads to the fol lowing recurrence relation: T(n) = T(n/2) + 
2 (where the second 2 represents the 2 recursive cal ls or two 
iterations of the loop. ) 
 What is the time complexity for this recurrence relation? 
Algorithms - Arora Euclid's Algorithm - GCD 5
ANALYZING EUCLID’S ALGORITHM 
 T(n) = T(n/2) + 2*(time to do modulo operation) 
 I f modulo operation is a constant time operation, then: 
 T(n) = T(n/2) + 2 
 T(n) = O(log n) 
 However, usually modulo operation is not real ly constant time, 
but takes the same time as number of bits, bytes or decimal 
digits (using long division) 
 In that case: 
 T(n) = T(n/2) + 2*log n 
  T(n) = O(log2n) // This is (log n)2, not log log n 
Algorithms - Arora Euclid's Algorithm - GCD 6
LOG2(N): LOGARITHMIC OR QUADRATIC? 
 log(n) factor is a bit misleading. 
 This is because we are used to looking at n as the size of the 
input. For example, given n numbers in sor ted order, binary 
search can find the input in O( log n) time. In that case, n 
represents the SIZE OF THE INPUT (2n or 4n bytes, etc) 
 However, i n c a s e o f E u c l id’ s a l g o rit hm, n i s t h e input value, 
not the size of the input . This is fundamentally dif ferent. 
 Given value of n can be encoded in log n bits. (binary 
encoding) 
 T h a t i s , t h e E u c l id’ s a l g o r ithm r u ns i n l o g2n time, when given 
log n bits. That is, it runs in time that is quadratically related 
to the input size. 
Algorithms - Arora Euclid's Algorithm - GCD 7
ANALYZING EUCLID’S ALGORITHM (CONT. ) 
 log(n) is the size of the input (propor tional to the number of 
bits, bytes or decimal digits) 
 Gi ve n s b i t s , E u c l id’ s a l g o r ithm wi l l fi n i s h i n O( s2) time. 
 [When in confusion, always think of the size of the input as 
your determining factor. ] 
Algorithms - Arora Euclid's Algorithm - GCD 8
TIGHTNESS OF ANALYSIS 
 To be precise, we have only shown that our analysis of 
E u c l id’ s a l g o r it hm i s l o g2n time. Let us revisit our analysis 
 Relationship between n and r 
 r < m 
 Also, r  n - m 
  r < n/2 
 Is our analysis tight? In other words, are there some values of 
n and m, such gcd(n,m) actually requires log n steps? 
 Looking for counter example: 
 n = 1000, m = 50. r=0. Only takes 1 step, not log 1000 steps. 
 n = 1000, m = 417. r=166. In next iteration, n = 417, m = 166, r = 
85. In next iteration, n = 166, m = 85, r = 81. In next iteration, n = 
85, m = 81, r = 4. In next iteration, n = 81, m = 4, r = 1. Only took 4 
iterations. 
Algorithms - Arora Euclid's Algorithm - GCD 9
TIGHTNESS OF ANALYSIS (CONT. ) 
 Using induction, one can prove that if n and m are Fibonacci 
numbers Fk+2 and Fk+1, E u c li d’ s a l g o ri thm t a kes k s te p s . 
 Proof : 
 n = Fk+2 
 m = Fk+1 
 r = Fk+2 – Fk+1 = Fk 
 Using induction hypothesis, steps(m,r) = k-1 
 We know that Fk is propor tional to k, where  is the golden 
ratio. 
 T h e r efo re, u s i ng two F i b o na cc i numb e r s n a nd m, E u c l id’ s 
algorithm can take log (n) steps. 
 I t is also possible to prove that the two successive Fibonacci 
n umb e r s p r e s e n t t h e wo r s t c a s e fo r t h e E u c l id’ s a l g o r ithm. 
Algorithms - Arora Euclid's Algorithm - GCD 10
CONCLUSIONS 
 T h e a na l y si s o f E u c l id’ s a l g o r it hm i s t i g h t . 
 T h e r e fo re , i n t h e wo r s t c a s e , E u c l id’ s a l g o r ithm c a n i n d e e d 
take O( log n) steps. 
 Modulo operation takes time propor tional to the number of 
bits/bytes/decimal digits. 
 Gi ve n va l u e s n a n d m, tot a l t ime o f E u c l id’ s a l g o r it hm i s 
bounded by O( log2n) . 
 Gi ve n s b i t s / by te s/ de c imal d i g it s, E u c l id ’ s a l g o r it hm t a ke s 
O(s2) time. 
Algorithms - Arora Euclid's Algorithm - GCD 11
REFERENCES 
 http://www.standardwisdom.com/algorithms-book 
 http://en.wikipedia.org/wiki/Euclidean_algorithm 
 http://www.cut -the-knot.org/blue/Euclid.shtml 
 http://www.albany.edu/~csi503/pdfs/lect05.pdf 
Algorithms - Arora Euclid's Algorithm - GCD 12

More Related Content

PPTX
Eucledian algorithm for gcd of integers and polynomials
PPTX
Discrete Mathematics Presentation
PPT
5 csp
PPTX
Stressen's matrix multiplication
PPT
Asymptotic notations
PDF
Little o and little omega
PPT
Data encryption standard
PDF
I.BEST FIRST SEARCH IN AI
Eucledian algorithm for gcd of integers and polynomials
Discrete Mathematics Presentation
5 csp
Stressen's matrix multiplication
Asymptotic notations
Little o and little omega
Data encryption standard
I.BEST FIRST SEARCH IN AI

What's hot (20)

PPTX
Modular arithmetic
PPTX
Topic1 substitution transposition-techniques
PPTX
Complexity analysis in Algorithms
PDF
Dbms 14: Relational Calculus
PPTX
First order logic
PPTX
Predicate logic
PDF
I. Alpha-Beta Pruning in ai
PPTX
Mathematical Analysis of Recursive Algorithm.
PPTX
Merge sort algorithm
PPT
Polyalphabetic Substitution Cipher
PPT
BackTracking Algorithm: Technique and Examples
PPTX
Decision tree induction \ Decision Tree Algorithm with Example| Data science
PPTX
introduction to division algorithm
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPTX
PPT
Number theory
PPTX
number theory.ppt
PPT
Version spaces
PPTX
Error Finding in Numerical method
DOCX
SCSJ3553 - Artificial Intelligence Final Exam paper - UTM
Modular arithmetic
Topic1 substitution transposition-techniques
Complexity analysis in Algorithms
Dbms 14: Relational Calculus
First order logic
Predicate logic
I. Alpha-Beta Pruning in ai
Mathematical Analysis of Recursive Algorithm.
Merge sort algorithm
Polyalphabetic Substitution Cipher
BackTracking Algorithm: Technique and Examples
Decision tree induction \ Decision Tree Algorithm with Example| Data science
introduction to division algorithm
DESIGN AND ANALYSIS OF ALGORITHMS
Number theory
number theory.ppt
Version spaces
Error Finding in Numerical method
SCSJ3553 - Artificial Intelligence Final Exam paper - UTM
Ad

Viewers also liked (20)

PPTX
Algorithmic Puzzles
PPTX
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
PPTX
Graph Traversal Algorithms - Breadth First Search
PPTX
Dynamic Programming - Part II
PPTX
NP completeness
PPTX
Dynamic Programming - Part 1
PPTX
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
PPTX
BTrees - Great alternative to Red Black, AVL and other BSTs
PPTX
NP-Completeness - II
PPTX
Greedy Algorithms
PPTX
Online Algorithms - An Introduction
PDF
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
PDF
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
PPTX
Binomial Heaps and Fibonacci Heaps
PPTX
Splay Trees and Self Organizing Data Structures
PPTX
Tries - Tree Based Structures for Strings
PPTX
Online algorithms in Machine Learning
PPTX
Introduction to Algorithms and Asymptotic Notation
PPTX
Asymptotic Notation and Data Structures
Algorithmic Puzzles
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Breadth First Search
Dynamic Programming - Part II
NP completeness
Dynamic Programming - Part 1
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
BTrees - Great alternative to Red Black, AVL and other BSTs
NP-Completeness - II
Greedy Algorithms
Online Algorithms - An Introduction
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Binomial Heaps and Fibonacci Heaps
Splay Trees and Self Organizing Data Structures
Tries - Tree Based Structures for Strings
Online algorithms in Machine Learning
Introduction to Algorithms and Asymptotic Notation
Asymptotic Notation and Data Structures
Ad

Similar to Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis (20)

PDF
digital design and algorithm module 1 ppt
PDF
design and analysis of algorithm
PPTX
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
PPT
Kk20503 1 introduction
PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
PDF
Notion of Algorithms.pdf
PPTX
GCD of n Numbers
PPT
MFCS-17.ppt
PDF
Is unit 4_number_theory
PPT
ADVANCED ALGORITHMS-UNIT-3-Final.ppt
PPTX
DAA - UNIT 4 - Engineering.pptx
PPT
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
PPT
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
PPT
data unit notes from department of computer science
PPTX
01 - Introduction to Algorithms.pptx
PPTX
1.introduction analysis and design of algorithms(1).pptx
PDF
Introduction to Algorithm Design and Analysis.pdf
PPTX
Euclidean_Algorithm_Num_Theory_Presentation.pptx
PPTX
Euclidean_Algorithm_NT_Presentation.pptx
PPTX
It is about the number theory concept and integers
digital design and algorithm module 1 ppt
design and analysis of algorithm
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
Kk20503 1 introduction
Presentation_23953_Content_Document_20240906040454PM.pptx
Notion of Algorithms.pdf
GCD of n Numbers
MFCS-17.ppt
Is unit 4_number_theory
ADVANCED ALGORITHMS-UNIT-3-Final.ppt
DAA - UNIT 4 - Engineering.pptx
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
data unit notes from department of computer science
01 - Introduction to Algorithms.pptx
1.introduction analysis and design of algorithms(1).pptx
Introduction to Algorithm Design and Analysis.pdf
Euclidean_Algorithm_Num_Theory_Presentation.pptx
Euclidean_Algorithm_NT_Presentation.pptx
It is about the number theory concept and integers

More from Amrinder Arora (10)

PDF
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
PDF
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
PDF
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
PPTX
Divide and Conquer - Part 1
PPTX
Set Operations - Union Find and Bloom Filters
PPTX
R-Trees and Geospatial Data Structures
PPTX
Binary Search Trees - AVL and Red Black
PPTX
Graphs, Trees, Paths and Their Representations
PPTX
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
PPTX
Learning to learn
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Divide and Conquer - Part 1
Set Operations - Union Find and Bloom Filters
R-Trees and Geospatial Data Structures
Binary Search Trees - AVL and Red Black
Graphs, Trees, Paths and Their Representations
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Learning to learn

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Electronic commerce courselecture one. Pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
Electronic commerce courselecture one. Pdf
Understanding_Digital_Forensics_Presentation.pptx
A Presentation on Artificial Intelligence
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx

Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis

  • 1. EUCLID’S ALGORITHM FOR FINDING GREATEST COMMON DIVISOR
  • 2. EUCLID’S ALGORITHM (Quick history of this recently discovered algorithm) Euclid's Algorithm appears as the solution to the Proposition VII.2 in the Elements (written around 300 BC): Given two numbers not prime to one another, to find their greatest common measure. What Euclid called "common measure" is termed nowadays a common factor or a common divisor. Euclid VII.2 then offers an algorithm for finding the greatest common divisor(gcd) of two integers. Not surprisingly, the algorithm bears Euclid's name. Algorithms - Arora Euclid's Algorithm - GCD 2
  • 3. EUCLID’S ALGORITHM (CONT. ) // Preconditions: n > m > 0 // Return an error if preconditions are not met. // n%m is the remainder after dividing n with m. Recursive version gcd(n,m) r = n%m if r == 0 return m // else return gcd(m, r) Iterative version gcd(n,m) r = n%m while (r > 0) { n = m m = r r = n%m } return m Algorithms - Arora Euclid's Algorithm - GCD 3
  • 4. EUCLID’S ALGORITHM (CONT. ) The algorithm is based on the following two observations: 1. If m divides n, then gcd(n, m) = n. 2. If n = mk + r, for integers k and r, then gcd(n, m) = gcd(m, r). Indeed, every common divisor of n and m also divides r. Thus gcd(n, m) divides r. But, of course, gcd(n, m)|m. Therefore, gcd(n, m) is a common divisor of m and r and hence gcd(n, m) ≤ gcd(m, r). The reverse is also true because every divisor of m and r also divides n. Algorithms - Arora Euclid's Algorithm - GCD 4
  • 5. ANALYZING EUCLID’S ALGORITHM  What is the relationship between n, m and r?  There is no known relationship between n and m, other than the precondition that n > m.  But we can claim something more interesting between n and r  r < m  Also, r  n - m   r < n/2  Af ter 2 recursive cal ls (or two iterations of the loop) , the first argument is no more than hal f .  This leads to the fol lowing recurrence relation: T(n) = T(n/2) + 2 (where the second 2 represents the 2 recursive cal ls or two iterations of the loop. )  What is the time complexity for this recurrence relation? Algorithms - Arora Euclid's Algorithm - GCD 5
  • 6. ANALYZING EUCLID’S ALGORITHM  T(n) = T(n/2) + 2*(time to do modulo operation)  I f modulo operation is a constant time operation, then:  T(n) = T(n/2) + 2  T(n) = O(log n)  However, usually modulo operation is not real ly constant time, but takes the same time as number of bits, bytes or decimal digits (using long division)  In that case:  T(n) = T(n/2) + 2*log n   T(n) = O(log2n) // This is (log n)2, not log log n Algorithms - Arora Euclid's Algorithm - GCD 6
  • 7. LOG2(N): LOGARITHMIC OR QUADRATIC?  log(n) factor is a bit misleading.  This is because we are used to looking at n as the size of the input. For example, given n numbers in sor ted order, binary search can find the input in O( log n) time. In that case, n represents the SIZE OF THE INPUT (2n or 4n bytes, etc)  However, i n c a s e o f E u c l id’ s a l g o rit hm, n i s t h e input value, not the size of the input . This is fundamentally dif ferent.  Given value of n can be encoded in log n bits. (binary encoding)  T h a t i s , t h e E u c l id’ s a l g o r ithm r u ns i n l o g2n time, when given log n bits. That is, it runs in time that is quadratically related to the input size. Algorithms - Arora Euclid's Algorithm - GCD 7
  • 8. ANALYZING EUCLID’S ALGORITHM (CONT. )  log(n) is the size of the input (propor tional to the number of bits, bytes or decimal digits)  Gi ve n s b i t s , E u c l id’ s a l g o r ithm wi l l fi n i s h i n O( s2) time.  [When in confusion, always think of the size of the input as your determining factor. ] Algorithms - Arora Euclid's Algorithm - GCD 8
  • 9. TIGHTNESS OF ANALYSIS  To be precise, we have only shown that our analysis of E u c l id’ s a l g o r it hm i s l o g2n time. Let us revisit our analysis  Relationship between n and r  r < m  Also, r  n - m   r < n/2  Is our analysis tight? In other words, are there some values of n and m, such gcd(n,m) actually requires log n steps?  Looking for counter example:  n = 1000, m = 50. r=0. Only takes 1 step, not log 1000 steps.  n = 1000, m = 417. r=166. In next iteration, n = 417, m = 166, r = 85. In next iteration, n = 166, m = 85, r = 81. In next iteration, n = 85, m = 81, r = 4. In next iteration, n = 81, m = 4, r = 1. Only took 4 iterations. Algorithms - Arora Euclid's Algorithm - GCD 9
  • 10. TIGHTNESS OF ANALYSIS (CONT. )  Using induction, one can prove that if n and m are Fibonacci numbers Fk+2 and Fk+1, E u c li d’ s a l g o ri thm t a kes k s te p s .  Proof :  n = Fk+2  m = Fk+1  r = Fk+2 – Fk+1 = Fk  Using induction hypothesis, steps(m,r) = k-1  We know that Fk is propor tional to k, where  is the golden ratio.  T h e r efo re, u s i ng two F i b o na cc i numb e r s n a nd m, E u c l id’ s algorithm can take log (n) steps.  I t is also possible to prove that the two successive Fibonacci n umb e r s p r e s e n t t h e wo r s t c a s e fo r t h e E u c l id’ s a l g o r ithm. Algorithms - Arora Euclid's Algorithm - GCD 10
  • 11. CONCLUSIONS  T h e a na l y si s o f E u c l id’ s a l g o r it hm i s t i g h t .  T h e r e fo re , i n t h e wo r s t c a s e , E u c l id’ s a l g o r ithm c a n i n d e e d take O( log n) steps.  Modulo operation takes time propor tional to the number of bits/bytes/decimal digits.  Gi ve n va l u e s n a n d m, tot a l t ime o f E u c l id’ s a l g o r it hm i s bounded by O( log2n) .  Gi ve n s b i t s / by te s/ de c imal d i g it s, E u c l id ’ s a l g o r it hm t a ke s O(s2) time. Algorithms - Arora Euclid's Algorithm - GCD 11
  • 12. REFERENCES  http://www.standardwisdom.com/algorithms-book  http://en.wikipedia.org/wiki/Euclidean_algorithm  http://www.cut -the-knot.org/blue/Euclid.shtml  http://www.albany.edu/~csi503/pdfs/lect05.pdf Algorithms - Arora Euclid's Algorithm - GCD 12