This preview shows page 1. Sign up to view the full content.
Unformatted text preview: es the Largest Common Prime of 2 numbers c) Computes the Greatest Common Factor of 2 numbers Mystery Algorithm What does the algorithm do?
remainder(x, y) 1. Request a value for x 2. Request a value for y 3. while x y do 4. assign x y to x 5. return x 6. end Request a value for a Request a value for b if a b then assign b to c if a < b then assign a to c while (remainder(a, c)0 OR remainder(b, c) 0) subtract one from c return c end a) Computes the Least Common Denominator of 2 fractions b) Computes the Largest Common Prime of 2 numbers c) Computes the Greatest Common Factor of 2 numbers We can now build from our Remainder algorithm
Greatest Common Factor
Note: Writing remainder(a,b) means a is provided as the first value to the remainder algorithm and b as the second value. 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end Demo of "Greatest Common Factor"
1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end a b c Demo of "Greatest Common Factor"
1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end a b c 16 Demo of "Greatest Common Factor"
1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end a b c 16 12 Demo of "Greatest Common Factor"
1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 Demo of "Greatest Common Factor"
1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 Demo of "Greatest Common Factor"
1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one...
View Full
Document
 Fall '07
 Ringenberg
 Algorithms

Click to edit the document details