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

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

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 ff 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 first statement handling the case " a = 0 or b = 0 ".) 3. [Not graded] Begin by making

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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern