Exercise2-95 <---> Exercise2-97

Exercise 2.96

Implement the procedure pseudoremainder-terms, which is just like remainder-terms except that it multiplies the dividend by the integerizing factor described above before calling div-terms. Modify gcd-terms to use pseudoremainder-terms, and verify that greatest-common-divisor now produces an answer with integer coefficients on the example in exercise 2.95.

b. The GCD now has integer coefficients, but they are larger than those of P1. Modify gcd-terms so that it removes common factors from the coefficients of the answer by dividing all the coefficients by their (integer) greatest common divisor.


Scheme solution:

  (define (pseudoremainder-terms T1 T2)
    (let ((integerizingfactor (exponent (coeff (first-term T2)) (+ 1 (- (order (first-term T1)) (order (first-term T2)))))))
      (let ((factorizedT1 (mul-terms T1 (adjoin-term (make-term 0 integerizingfactor) (the-empty-termlist)))))
        (cadr (div-terms factorizedT1 T2)))))
  
  (define (get-gcd T)
    (if (eqv? (rest-terms T) (the-empty-termlist))
        (coeff (first-term T))
        (gcd (coeff (first-term T)) (get-gcd (rest-terms T)))))
             
  (define (normalize-by-gcd T)
    (div-terms T (adjoin-term (make-term 0 (get-gcd T)) (the-empty-termlist))))
  
  (define (gcd-terms a b)
    (if (empty-termlist? b)
        (normalize-by-gcd a)
        (gcd-terms b (pseudoremainder-terms a b))))

Exercise2-95 <---> Exercise2-97


Comments


:) :)) :( ;) :\ |) X-( B) Markup

Exercise2-96 (last edited 2010-04-13 23:27:56 by SriramDevadas)