{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Recursion1.4

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

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

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 ) a b b a b = 3. Algorithm halts, since 0 mod a b b < 1. 0 gcd( , ) gcd( ,0) b a b a a = = =

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

View Full Document
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 b a b b = 2nd argument drops by factor of at least 2 every 2 itera C t laim: ions. Proof : Iteration Arg 1 Arg 2 1 mod 2 mod mod( mod ) i a b i b a b i a b b a b + + < mod /2. Then mod( mo Ca d se ) d /2 o 1 m : a b b b a b a b b mod /2. Then mod( mod ) / Case 2: 2 b a b b b a b b > > < ü࿏ ü࿏
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 b a b b = Let total number of recursive calls to Euclid. k = Each stackframe must compute mod , which takes more than constant time. a b 2 It can be shown that the resulting time complexity is

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online