Comment 3 for bug 180170

Revision history for this message
Jens Axel Søgaard (soegaard) wrote : Re: [Bug 180170] Missing bignum sqrt

Abdulaziz Ghuloum wrote:
> Is it possible that you actually wanted exact-integer-sqrt?

In the case of the factorization function sqrt was used
used to break out of a loop early, so it doesn't
matter which one of sqrt and integer-sqrt is used here.

Grepping for sqrt reveals the following use, where
it is not possible to replace sqrt with integer-sqrt.

   (define (second-degree-solutions a b c)
     ; return list of solutions to a x^2 + b x + c = 0
     (let ([d (- (* b b) (* 4 a c))])
       (cond
         [(< d 0)
          '()]
         [(= d 0)
          (list (/ b (* -2 a)))]
         [else
          (let ([sqrt-d (sqrt d)])
            (list (/ (- (- b) sqrt-d) (* 2 a))
                  (/ (+ (- b) sqrt-d) (* 2 a))))])))

It is used like this to examine whether a number is
triangular, pentagonal, etc.

   (define (natural? n)
     (and (integer? n) (positive? n)))

   (define (second-degree-natural-solutions a b c)
     ; return list of integer solutions to a x^2 + b x + c = 0
     (filter natural? (second-degree-solutions a b c)))

   (define (triangle? n)
     (not (null? (second-degree-natural-solutions 1/2 1/2 (- n)))))

   (define (pentagonal? n)
     (not (null? (second-degree-natural-solutions 3/2 -1/2 (- n)))))

Hmm. I suppose one could use the following definition of s instead
of sqrt.

(define (square n)
   (* n n))

(define (s n)
   (let ([sqrt-n (integer-sqrt n)])
     (if (= (square sqrt-n) n)
         sqrt-n
         (sqrt (exact->inexact n)))))

/Jens Axel