Recursion1.4 - Euclids Algorithm (circa 300 BC) Euclid(a,b)...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 21 - Euclid’s Algorithm ( circa 300 BC) Euclid(a,b) <Precondition: a and b are non-negative integers> <Postcondition: returns gcd( a , b )> if b = 0 then return( a ) else return(Euclid( b , a mod b )) Precondition met, since a mod b ! ! Postcondition met, since 2. Otherwise, gcd( , ) gcd( , mod ) ab ba b = 3. Algorithm halts, since 0 mod b < 1. 0 gcd( , ) gcd( ,0) b a a = = =
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 22 - Time Complexity return( E ) uclid(a, if 0 then else return(Euclid( , mod )) b ) a ba b b = 2nd argument drops by factor of at least 2 every 2 itera C t laim: ions. Proof : Iteration Arg 1 Arg 2 1m o d 2m o d m o d ( m o d ) ia b ib a b b b a b + + < mod / 2. Then mod( mo Ca d se )d / 2 o 1 m : ab b b b mod / 2. Then mod( mod ) / Case 2: 2 bb b a b b >> < ü ü
Background image of page 2
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 23 - Time Complexity return( E ) uclid(a, if 0 then else return(Euclid( , mod )) b ) a ba b b = Let total number of recursive calls to Euclid. k = Each stackframe must compute mod , which takes more than constant time. ab 2 It can be shown that the resulting time complexity is
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

Page1 / 5

Recursion1.4 - Euclids Algorithm (circa 300 BC) Euclid(a,b)...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online