sol1 - CMPSC 465 SOLUTIONS TO ASSIGNMENT 1 This assignment...

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

View Full Document Right Arrow Icon
CMPSC 465 SOLUTIONS TO ASSIGNMENT 1 This assignment is worth a total of 100 points Fall 2008 1. [14 Points] (a) We are to compute gcd(46139 , 47027) by applying Euclid’s algorithm, that is via a series of integer divisions with remainders gcd(46139 , 47027) 46139 = 47027 × 0 + 46139 = gcd(47027 , 46139) 47027 = 46139 × 1 + 888 = gcd(46139 , 888) 46139 = 888 × 51 + 851 = gcd(888 , 851) 888 = 851 × 1 + 37 = gcd(851 , 37) 851 = 37 × 23 + 0 = gcd(37 , 0) = 37 [Canceled] (b) Compare with the algorithm that checks consecutive integers from 46139 to 37. Each of the 46139 - 36 = 46133 integers in this range will be tested as divisor of both 46139 and 47027, which requires one or two divisions. The total number of divisions thus lies between 46133 and 2 × 46133 = 92266 . 2. [12 Points] We are to write the version of Euclid’s algorithm that uses only subtractions instead of divisions. There are several ways of doing this. For example: Algorithm Euclid 2( m, n ) // Input: Integers m, n 0 // Output: gcd( m, n ) while n ± =0 do if m<n then swap ( m, n ) ; // temp m ; m n ; n temp m m - n ; return m Alternatively, if both input numbers are assumed to be positive, then a 0 can only occur as di±erence of two equal terms, and we could stop there: Algorithm Euclid 3( m, n ) // Input: Integers m, n > 0 // Output: gcd( m, n ) while m ± = n do if m>n then m m - n else n n - m ; return m (This algorithm gets stuck if one of the numbers is 0, which is why it requires input m, n > 0 or an additional ²rst statement handling the case " a or b ".) 3. [Not graded] Begin by making
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/01/2008 for the course CMPSC 465 taught by Professor Burchardcharles during the Fall '08 term at Penn State.

Page1 / 2

sol1 - CMPSC 465 SOLUTIONS TO ASSIGNMENT 1 This assignment...

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

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